From b6ffb80e3f6fa4da4506a782067d809c7f18ab1d Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 5 Aug 2022 10:29:50 +0430 Subject: update --- 377/main.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 377/main.js 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)); -- cgit v1.2.3