We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents ce77326 + 688a9ab commit 7a08d09Copy full SHA for 7a08d09
dynamic_programming/minimum_partition.py
@@ -0,0 +1,28 @@
1
+"""
2
+Partition a set into two subsets such that the difference of subset sums is minimum
3
4
+def findMin(arr):
5
+ n = len(arr)
6
+ s = sum(arr)
7
+
8
+ dp = [[False for x in range(s+1)]for y in range(n+1)]
9
10
+ for i in range(1, n+1):
11
+ dp[i][0] = True
12
13
+ for i in range(1, s+1):
14
+ dp[0][i] = False
15
16
17
+ for j in range(1, s+1):
18
+ dp[i][j]= dp[i][j-1]
19
20
+ if (arr[i-1] <= j):
21
+ dp[i][j] = dp[i][j] or dp[i-1][j-arr[i-1]]
22
23
+ for j in range(s/2, -1, -1):
24
+ if dp[n][j] == True:
25
+ diff = s-2*j
26
+ break;
27
28
+ return diff
0 commit comments