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

494 Target Sum

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

const targetSum = (nums, target) => {
  const dp = {}
  const backtrack = (index, currSum) => {
    const pos = index + '->' + currSum
    if (pos in dp) return dp[pos]
    if (index == nums.length) {
      if (currSum == target) {
        return 1
      } else {
        return 0
      }
    }
    dp[pos] = backtrack(index + 1, currSum + nums[index]) + backtrack(index + 1, currSum - nums[index])
    return dp[pos]
  }
  return backtrack(0, 0)
}

console.log(targetSum([1, 1, 1, 1, 1], 3))

Created 2022-03-23T17:46:05+00:00 · Edit