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

03 Can Sum

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

console.time('canSum1')
var findTargetSumWays = function(nums, target) {
  if (target === 0) return true
  if (target < 0) return false
  for (let num of nums) {
    const remainder = target - num
    if (findTargetSumWays(nums, remainder) === true) {
      return true
    }
  }
  return false
}
console.log(findTargetSumWays([5, 55, 100], 556))
console.timeEnd('canSum1') //40913.223ms

console.time('canSum2')
var findTargetSumWays2 = function(nums, target, dp = {}) {
  if (target in dp) return dp[target]
  if (target === 0) return true
  if (target < 0) return false
  for (let num of nums) {
    const remainder = target - num
    if (findTargetSumWays2(nums, remainder, dp) === true) {
      dp[target] = true
      return true
    }
  }
  dp[target] = false
  return false
}
console.log(findTargetSumWays2([5, 55, 100], 556))
console.timeEnd('canSum2') //1.220ms

Created 2021-10-14T00:53:46+01:00 · Edit