diff options
author | terminaldweller <thabogre@gmail.com> | 2022-08-05 05:59:50 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-08-05 05:59:50 +0000 |
commit | b6ffb80e3f6fa4da4506a782067d809c7f18ab1d (patch) | |
tree | 3f34034d5bef33512cf4dd9a28c09b6b20253deb | |
parent | update (diff) | |
download | leetcode-b6ffb80e3f6fa4da4506a782067d809c7f18ab1d.tar.gz leetcode-b6ffb80e3f6fa4da4506a782067d809c7f18ab1d.zip |
update
-rwxr-xr-x | 377/main.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/377/main.js b/377/main.js new file mode 100755 index 0000000..2de906c --- /dev/null +++ b/377/main.js @@ -0,0 +1,38 @@ +#!/home/devi/.bun/bin/bun + +var combinationSum4 = function (nums, target, memo = {}) { + let sum = 0; + if (target in memo) { + return sum + memo[target]; + } + if (target === 0) { + sum++; + return sum; + } + if (target < 0) { + return sum; + } + + for (let num of nums) { + var remainder = target - num; + if (remainder === 0) { + sum++; + if (remainder in memo) { + memo[remainder]++; + } else { + memo[remainder] = 1; + } + } + if (remainder > 0) { + memo[remainder] = combinationSum4(nums, remainder, memo); + sum += memo[remainder]; + } + } + + console.log(memo); + memo[target] = sum; + return sum; +}; + +console.log(combinationSum4([1, 2, 3], 4)); +console.log(combinationSum4([4, 2, 1], 32)); |