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 cfb7db6 commit 8449524Copy full SHA for 8449524
combination-sum/yhkee0404.ts
@@ -0,0 +1,24 @@
1
+function combinationSum(candidates: number[], target: number, dp = new Map()): number[][] {
2
+ if (target <= 0) {
3
+ return [];
4
+ }
5
+ let ans = dp.get(target);
6
+ if (ans !== undefined) {
7
+ return ans;
8
9
+ ans = [];
10
+ for (const candidate of candidates) {
11
+ if (target == candidate) {
12
+ ans.push([candidate]);
13
+ continue;
14
15
+ for (const combination of combinationSum(candidates, target - candidate, dp)) {
16
+ if (combination[combination.length - 1] > candidate) {
17
18
19
+ ans.push([...combination, candidate]);
20
21
22
+ dp.set(target, ans);
23
24
+};
0 commit comments