-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubsetSum.py
More file actions
30 lines (22 loc) · 849 Bytes
/
subsetSum.py
File metadata and controls
30 lines (22 loc) · 849 Bytes
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
"""
This solution can be optimized by using an array of size [2][sum + 1] instead of an array of size[n + 1][sum + 1]. Since each iteration on i only uses the row before it you don't need to keep track of every row before a given row, just the one before it.
"""
def subsetSumDP(set, sum):
n = len(set)
S = [[False for i in range(sum + 1)] for i in range(n + 1)]
for i in range(n + 1):
for j in range(sum + 1):
if j == 0:
S[i][j] = True
elif i > 0:
subIndex = j - set[i - 1]
if subIndex >= 0:
S[i][j] = S[i - 1][j] or S[i][subIndex]
else:
S[i][j] = S[i - 1][j]
else:
S[i][j] = False
return S[n][sum]
S = [3, 34, 4, 12, 5, 2]
sum = 23
print(subsetSumDP(S, sum))