Skip to content

Commit d745086

Browse files
week 3: combination sum
1 parent 744656e commit d745086

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

combination-sum/prograsshopper.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
# Time complexity: O(n ^ (T / m))
4+
result = []
5+
6+
def dfs(remain_sum, index, path):
7+
if remain_sum < 0:
8+
return
9+
if remain_sum == 0:
10+
result.append(path)
11+
return
12+
13+
for i in range(index, len(candidates)):
14+
dfs(remain_sum - candidates[i], i, path + [candidates[i]])
15+
16+
dfs(target, 0, [])
17+
return result

0 commit comments

Comments
 (0)