-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2.py
48 lines (41 loc) · 1.63 KB
/
p2.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# User function Template for python3
class Solution:
def minDiffernce(self, arr, n):
total = sum(arr)
print("the tatal is ", total)
arr.sort()
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'])
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]
# {
# Driver Code Starts
# Initial Template for Python 3
if __name__ == '__main__':
arr = [12, 528, 129, 376, 504, 543, 363, 213, 138, 206, 440, 504, 418]
ob = Solution()
ans = ob.minDiffernce(arr, len(arr))
print(ans)