Skip to content

Commit 6b7bef5

Browse files
committed
adding combination sum
1 parent da6507b commit 6b7bef5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

combination-sum/daiyongg-kim.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
candidates.sort()
4+
result = []
5+
6+
def backtracking(start_index, current_combination, current_sum):
7+
if current_sum == target:
8+
result.append(list(current_combination))
9+
return
10+
11+
if current_sum > target:
12+
return
13+
14+
for i in range(start_index, len(candidates)):
15+
candidate = candidates[i]
16+
17+
if current_sum + candidate > target:
18+
break
19+
20+
current_combination.append(candidate)
21+
backtracking(i, current_combination, current_sum + candidate)
22+
23+
current_combination.pop()
24+
25+
backtracking(0, [], 0)
26+
return result

0 commit comments

Comments
 (0)