LeetCode: Combination Sum IV Solution
1/**2 * @param {number[]} nums3 * @param {number} target4 * @return {number}5 */6var combinationSum4 = function (nums, target) {7 const recursion = (accumulatedSum, memo = new Map()) => {8 if (memo.has(accumulatedSum)) {9 return memo.get(accumulatedSum)10 }1112 if (accumulatedSum > target) {13 return 014 }1516 if (accumulatedSum === target) {17 return 118 }1920 let res = 021 for (const num of nums) {22 res += recursion(accumulatedSum + num, memo)23 }24 memo.set(accumulatedSum, res)2526 return res27 }2829 return recursion(0)30}
Comments
Loading comments...
Tags
leetcode
recursion
dynamic programming
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.