-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum.py
More file actions
27 lines (19 loc) · 790 Bytes
/
combinationSum.py
File metadata and controls
27 lines (19 loc) · 790 Bytes
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
#good one
class Solution:
def combinationSum(self, candidates, target):
res = []
def helper(start_index, curr_combo, rest_target):
#success
if rest_target == 0:
res.append(curr_combo.copy())
#failure
if rest_target < 0:
return "invalid path"
for i in range(start_index, len(candidates)):
curr_combo.append(candidates[i])
helper(i, curr_combo, rest_target - candidates[i])
curr_combo.pop()
helper(0, [], target)
return res
myVar = Solution()
print(myVar.combinationSum([2,3,6,7], 7))