-
Notifications
You must be signed in to change notification settings - Fork 90
/
k Sum II.py
65 lines (49 loc) · 1.62 KB
/
k Sum II.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
60
61
62
63
64
65
"""
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target.
Example
Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions.
"""
__author__ = 'Danyang'
class Solution:
def kSumII(self, A, k, target):
"""
brute force dfs
Branch and prune
:param A: An integer array.
:param k: a positive integer (k <= length(A))
:param target: int
:return: int
"""
ret = []
# self.dfs(A, k, [], target, ret)
self.dfs_stk(A[::-1], k, [], target, ret)
return ret
def dfs(self, A, k, cur, remain, ret):
if len(cur) == k and remain == 0:
ret.append(list(cur))
if not A or len(cur) >= k or len(A)+len(cur) < k:
return
# save space, but need to do the clean up
num = A.pop(0)
self.dfs(A, k, cur, remain, ret)
cur.append(num)
self.dfs(A, k, cur, remain-num, ret)
cur.pop()
A.push(0, num)
def dfs_stk(self, A, k, cur, remain, ret):
"""
since array insert takes O(n), speed up by using stack
"""
if len(cur) == k and remain == 0:
ret.append(list(cur))
if not A or len(cur) >= k or len(A)+len(cur) < k:
return
# save space, but need to do the clean up
num = A.pop()
self.dfs(A, k, cur, remain, ret)
cur.append(num)
self.dfs(A, k, cur, remain-num, ret)
cur.pop()
A.append(num)
if __name__ == "__main__":
assert Solution().kSumII([1, 2, 3, 4], 2, 5) == [[3, 2], [1, 4]]