-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdailycoding042.py
59 lines (48 loc) · 1.54 KB
/
dailycoding042.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
49
50
51
52
53
54
55
56
57
58
59
"""
Given a list of integers S and a target number k,
write a function that returns a subset of S that adds up to k.
If such a subset cannot be made, then return null.
Integers can appear more than once in the list.
You may assume all numbers in the list are positive.
For example, given S = [12, 1, 61, 5, 9, 2] and k = 24,
return [12, 9, 2, 1] since it sums up to 24.
solution:
Use divide and conquer by considering subproblems of the array with k <- k - s[0].
If a solution exists for that subproblem, then s[0] is in the solution set and
recurse further, otherwise, recurse on the next subproblem with the sum unchanged.
Runs in O(N) time, O(N) space
"""
def subset_sum(s, k):
# base cases
if len(s) == 0:
return None
if s[0] == k:
return [s[0]]
# if the subproblem can be solved with the first element,
# then return the first element + the solution to the subproblem.
# Otherwise the first element is not in the solution, so solve the sub
# problem with the same current sum.
subsum = subset_sum(s[1:], k - s[0])
if subsum:
return [s[0]] + subsum
else:
return subset_sum(s[1:], k)
def main():
tests = (
([12, 1, 61, 5, 9, 2], 24),
([12, 1, 61, 5, 9, 2], 61),
([12, 1, 61, 5, -108, 2], -106),
([1,2,3], 40)
)
ans = (
[12, 1, 9, 2],
[61],
[-108, 2],
None
)
if all(subset_sum(*t) == a for t, a in zip(tests, ans)):
print("Passed")
else:
print("Failed")
if __name__ == '__main__':
main()