We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 80d9991 commit fcf0f05Copy full SHA for fcf0f05
1 file changed
โcombination-sum/gcount85.pyโ
@@ -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