Skip to content

Commit 9eb0fb8

Browse files
committed
solve : combination sum
1 parent ff42bed commit 9eb0fb8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

combination-sum/samthekorean.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# TC : O(n^2)
2+
# SC : O(n)
3+
class Solution:
4+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
5+
result = []
6+
7+
def dfs(start: int, total: int, current_combination: List[int]):
8+
if total > target:
9+
return
10+
if total == target:
11+
result.append(current_combination[:])
12+
return
13+
for i in range(start, len(candidates)):
14+
current_combination.append(candidates[i])
15+
dfs(i, total + candidates[i], current_combination)
16+
current_combination.pop()
17+
18+
dfs(0, 0, [])
19+
return result

0 commit comments

Comments
 (0)