#40

Combination Sum II

Medium Backtracking, use once, skip duplicates

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Each candidate may be used at most once and the list may contain duplicates. Return all unique combinations that sum to target.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: combinations to target, each number used once, input has repeats.

Why this works: recurse with i + 1 and skip equal values at the same level.

Example: [10, 1, 2, 7, 6, 1, 5], target 8 → sorted [1, 1, 2, 5, 6, 7, 10]

[1,1,6] [1,2,5] [1,7] [2,6]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using a HashSet of lists to de-duplicate — slow, and it hides the clean skip rule.

Solve on LeetCode ↗
Complexity
  • Time: O(2^n) worst case.
  • Space: O(n).
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon