code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

02 Count Of Subset Sum

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const numSubseq = (nums, target) => {
  const dp = new Map()
  const dfs = (index, currSum) => {
    const pos = index + '->' + currSum
    if (currSum == target) {
      return 1
    }
    if (index == nums.length) {
      return 0
    }
    if (dp.has(pos)) {
      return dp.get(pos)
    }
    let included = 0,
      excluded = 0
    if (nums[index] + currSum <= target) {
      included = dfs(index + 1, currSum + nums[index]) + dfs(index + 1, currSum)
    } else {
      excluded = dfs(index + 1, currSum)
    }
    const result = included + excluded
    dp.set(pos, result)
    return result
  }
  return dfs(0, 0)
}

console.log(numSubseq([2, 3, 5, 6, 8, 10], 10))

Created 2022-03-27T04:08:22+01:00 · Edit