Skip to content

Commit 57a7484

Browse files
committed
Week 03: combination-sum
1 parent 52e8667 commit 57a7484

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

combination-sum/mandel-17.py

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

0 commit comments

Comments
 (0)