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

08 Combination Sum

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

const combinationSum = (candidates, target) => {
  let result = []
  const backtrack = (index, curr, sum) => {
    if (sum === target) {
      result.push(curr.slice())
      return
    }
    if (sum > target) {
      return
    }
    for (let i = index; i < candidates.length; i++) {
      sum += candidates[i]
      curr.push(candidates[i])
      backtrack(i, curr, sum)
      curr.pop()
      sum -= candidates[i]
    }
  }
  backtrack(0, [], 0)
  return result
}

const combinationSum2 = (candidates, target) => {
  let result = []
  const backtrack = (arr, curr, sum) => {
    if (sum === target) {
      result.push(curr.slice())
      return
    }
    if (sum > target) {
      return
    }
    for (let i = 0; i < arr.length; i++) {
      sum += arr[i]
      curr.push(arr[i])
      backtrack(arr.slice(i), curr, sum)
      curr.pop()
      sum -= arr[i]
    }
  }
  backtrack(candidates, [], 0)
  return result
}

console.log(combinationSum([2, 3, 6, 7], 7))

Created 2022-02-26T02:08:30+00:00, updated 2022-02-26T16:34:43+00:00 · History · Edit