LeetCode: Combination Sum IV Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**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    }11
12    if (accumulatedSum > target) {13      return 014    }15
16    if (accumulatedSum === target) {17      return 118    }19
20    let res = 021    for (const num of nums) {22      res += recursion(accumulatedSum + num, memo)23    }24    memo.set(accumulatedSum, res)25
26    return res27  }28
29  return recursion(0)30}```

Tags

leetcode

recursion

dynamic programming

Next Post

LeetCode: Remove Nth Node From End Of List

Apr 19, 2021

Search Posts