Skip to content

Commit 5c4d5fb

Browse files
committed
combination sum solution
1 parent da3aad9 commit 5c4d5fb

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

combination-sum/yuseok89.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# TC: O(NlogN)
2+
# SC: O(logN)
3+
class Solution:
4+
5+
def rec(self, cur, candi, combi, sum, target, ans):
6+
7+
if sum == target:
8+
ans.append(combi)
9+
10+
return
11+
12+
for idx in range(cur, len(candi)):
13+
14+
multi = 1
15+
16+
while sum + multi * candi[idx] <= target:
17+
self.rec(idx + 1, candi, combi + [candi[idx]] * multi, sum + multi * candi[idx], target, ans)
18+
multi += 1
19+
20+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
21+
22+
ans = []
23+
24+
self.rec(0, candidates, [], 0, target, ans)
25+
26+
return ans
27+

0 commit comments

Comments
 (0)