-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path040_CombinationSum_II.py
37 lines (35 loc) · 1.32 KB
/
040_CombinationSum_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
class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
def combinationSum2Helper(candidates, target, start, res, candidate):
if not target:
res.append(candidate[:])
return
i = start
while i < size and candidates[i] <= target:
candidate.append(candidates[i])
combinationSum2Helper(candidates, target - candidates[i], i + 1, res, candidate)
candidate.pop()
i += 1
while i < size and candidates[i] == candidates[i - 1]: i+= 1
size = len(candidates)
res = list()
candidate = list()
candidates.sort()
combinationSum2Helper(candidates, target, 0, res, candidate)
return res
# A very interesting DP solution
def combinationSum2(self, candidates, target):
candidates.sort()
table = [None] + [set() for i in range(target)]
for i in candidates:
if i > target:
break
for j in range(target - i, 0, -1):
table[i + j] |= {elt + (i,) for elt in table[j]}
table[i].add((i,))
return map(list, table[target])