Skip to content

Commit fcf0f05

Browse files
committed
Add solution for Combination Sum problem
1 parent 80d9991 commit fcf0f05

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
# Intuition
3+
backtracking
4+
5+
# Complexity
6+
- Time complexity: N์„ ๊นŠ์ด๋งŒํผ ๊ณฑํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์„ ๋•Œ, O(N^(T/M))
7+
N = candidates ๊ฐœ์ˆ˜
8+
T = target
9+
M = candidates ์ค‘ ์ตœ์†Œ๊ฐ’
10+
11+
- Space complexity: O(T/M)
12+
"""
13+
14+
15+
class Solution:
16+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
17+
candidates.sort()
18+
answer = []
19+
output = []
20+
21+
def dfs(cur_i, cur_sum):
22+
if cur_sum == target:
23+
answer.append(output[:])
24+
return
25+
26+
for i in range(cur_i, len(candidates)):
27+
num = candidates[i]
28+
29+
if cur_sum + num > target:
30+
break
31+
32+
output.append(num)
33+
dfs(i, cur_sum + num)
34+
output.pop()
35+
36+
dfs(0, 0)
37+
return answer

0 commit comments

Comments
ย (0)