-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm39.py
35 lines (29 loc) · 1.04 KB
/
m39.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
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def combos(outputs: List[List[int]],
current: List[int] = [],
currentSum: int = 0,
candidates: List[int] = candidates) -> None :
if currentSum > target :
return
if currentSum == target :
outputs.append(current.copy())
return
if not candidates :
return
hold = candidates.pop()
combos(outputs, current, currentSum, candidates)
currentSum += hold
cnt = 0
while currentSum <= target :
current.append(hold)
cnt += 1
combos(outputs, current, currentSum, candidates)
currentSum += hold
for _ in range(cnt) :
current.pop()
candidates.append(hold)
candidates.sort()
output = []
combos(output)
return output