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 414efbc commit aa71427Copy full SHA for aa71427
combination-sum/HC-kang.ts
@@ -0,0 +1,19 @@
1
+// T.C: O(n^t) // n: candidates.length, t: target
2
+// S.C: O(n)
3
+function combinationSum(candidates: number[], target: number): number[][] {
4
+ const result: number[][] = [];
5
+ function dfs(start: number, target: number, path: number[]) {
6
+ if (target < 0) return;
7
+ if (target === 0) {
8
+ result.push([...path]);
9
+ return;
10
+ }
11
+ for (let i = start; i < candidates.length; i++) {
12
+ path.push(candidates[i]);
13
+ dfs(i, target - candidates[i], path);
14
+ path.pop();
15
16
17
+ dfs(0, target, []);
18
+ return result;
19
+};
0 commit comments