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

09 Combination Sum 2

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

const combinationSum2 = (candidates, target) => {
  candidates.sort((a, b) => a - b)
  const 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++) {
      if (i > index && candidates[i] === candidates[i - 1]) continue
      sum += candidates[i]
      curr.push(candidates[i])
      backtrack(i + 1, curr, sum)
      curr.pop()
      sum -= candidates[i]
    }
  }
  backtrack(0, [], 0)
  return result
}

console.log(combinationSum2([10, 1, 2, 7, 6, 1, 5], 8))

Created 2022-02-26T16:34:43+00:00 · Edit