Skip to content

Commit cc50e03

Browse files
authored
Create subset_sum_dp2.py
1 parent b22d5d4 commit cc50e03

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

dynamic-programming/subset_sum_dp2.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) June 02, 2017 CareerMonk Publications and others.
2+
3+
# Creation Date : 2017-06-02 06:15:46
4+
# Last modification : 2017-06-02
5+
# Modified by : Narasimha Karumanchi
6+
# Book Title : Algorithm Design Techniques
7+
# Warranty : This software is provided "as is" without any
8+
# warranty; without even the implied warranty of
9+
# merchantability or fitness for a particular purpose.
10+
11+
def subset_sum(A):
12+
n = len(A)
13+
K = 0
14+
for i in range(0, n):
15+
K += A[i]
16+
A.sort()
17+
T = [0] * ( K + 1 )
18+
T[0] = 1
19+
R = 0
20+
# process the numbers one by one
21+
for i in range(0, n):
22+
for j in range(R,-1, -1):
23+
if( T[j] ):
24+
T[j + A[i]] = 1
25+
R = min(K/2, R+A[i])
26+
return T[K / 2]
27+
28+
A = [3,2,4,19,3,7,13,10,6,11]
29+
print subset_sum(A)

0 commit comments

Comments
 (0)