Skip to content

Commit 00093a4

Browse files
committed
combination sum solution
1 parent 0911277 commit 00093a4

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

combination-sum/jylee2033.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
output = []
4+
5+
def backtrack(remain, comb, start):
6+
if remain == 0:
7+
output.append(list(comb))
8+
return
9+
10+
elif remain < 0:
11+
return
12+
13+
for i in range(start, len(candidates)):
14+
comb.append(candidates[i])
15+
backtrack(remain - candidates[i], comb, i)
16+
comb.pop()
17+
18+
backtrack(target, [], 0)
19+
return output
20+
21+
# Time Complexity : O(N^T/M) N - number of candidates, T - target, M - minimum value in candidates
22+
# Space Complexity : O(T/M) - maximum depth of the recursion tree

0 commit comments

Comments
 (0)