-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_array_equal_sum.rb
32 lines (30 loc) · 1.53 KB
/
sub_array_equal_sum.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
def canPartition(self, arr: List[int]) -> bool:
total= sum(arr)
memo = {}
memo['optimal_sub_sum'] = total
Solution.calculate_min_diff(memo, arr, 0, 0, total)
print(memo['optimal_sub_sum'])
# print(memo)
return abs(total - 2*memo['optimal_sub_sum']) == 0
def calculate_min_diff(memo, arr, i, current_total, total):
memo_key = str(i) + "_" + str(current_total)
if memo_key in memo:
return memo[memo_key]
if i >= len(arr) or current_total * 2 > total:
memo[memo_key] = total - current_total
if abs(total - 2*memo['optimal_sub_sum']) > abs(total - 2*memo[memo_key]):
memo['optimal_sub_sum'] = memo[memo_key]
return memo[memo_key]
# we have two choice, either we can consider the current item
# or skip the current item
with_current_item = Solution.calculate_min_diff(memo, arr, i+1, current_total+arr[i], total)
# with_current_item = Solution.calculate_min_diff(memo,arr, i+1, current_total+arr[i], total )
without_current_item = Solution.calculate_min_diff(memo, arr, i+1, current_total, total)
if abs(total - with_current_item * 2) > abs(total - without_current_item * 2):
memo[memo_key] = without_current_item
else:
memo[memo_key] = with_current_item
if abs(total - 2*memo['optimal_sub_sum']) > abs(total - 2*memo[memo_key]):
memo['optimal_sub_sum'] = memo[memo_key]
return memo[memo_key]