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 fe8e8bf commit 6cad65fCopy full SHA for 6cad65f
combination-sum/reach0908.js
@@ -0,0 +1,34 @@
1
+/**
2
+ * 시간복잡도 O(n^(T/m))
3
+ * 풀이방법: DFS 백트래킹
4
+ * 결과: 2ms
5
+ * @param {number[]} candidates
6
+ * @param {number} target
7
+ * @return {number[][]}
8
+ */
9
+const combinationSum = function (candidates, target) {
10
+ const result = [];
11
+ const nums = [];
12
+
13
+ const sortedCandidates = candidates.sort((a, b) => a - b);
14
15
+ function dfs(start, sum) {
16
+ if (sum === target) {
17
+ result.push([...nums]);
18
+ }
19
20
+ for (let i = start; i < sortedCandidates.length; i += 1) {
21
+ const num = sortedCandidates[i];
22
23
+ if (sum + num > target) break;
24
25
+ nums.push(num);
26
+ dfs(i, sum + num);
27
+ nums.pop();
28
29
30
31
+ dfs(0, 0);
32
33
+ return result;
34
+};
0 commit comments