Skip to content

Commit 3e593f4

Browse files
committed
Combination sum solution
1 parent 557a5af commit 3e593f4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

combination-sum/ohkingtaek.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
"""
4+
- 시간복잡도: O(n^2)
5+
- 공간복잡도: O(n)
6+
DFS 사용
7+
1. 모든 조합을 찾기
8+
2. 조합을 찾을 때, 중복된 조합을 방지하기
9+
"""
10+
result = []
11+
12+
def dfs(start, path, total):
13+
if total == target:
14+
result.append(path[:])
15+
return
16+
if total > target:
17+
return
18+
19+
for i in range(start, len(candidates)):
20+
path.append(candidates[i])
21+
dfs(i, path, total + candidates[i])
22+
path.pop()
23+
24+
dfs(0, [], 0)
25+
return result

0 commit comments

Comments
 (0)