#39

Combination Sum

Medium Backtracking with reuse

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

?The problem

Return all unique combinations of candidates (distinct numbers, each usable unlimited times) 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 that add to target, reusing numbers allowed.

Why this works: recurse with the same index to allow reuse; stop when a candidate is bigger than what's left.

Example: [2, 3, 6, 7], target 7

2+2+3 = 7 ✓7 = 7 ✓

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Recursing with start = 0 each time, which produces the same combination in different orders.

Solve on LeetCode ↗
Complexity
  • Time: Exponential — bounded by the number of combinations explored.
  • Space: O(target / min) recursion depth.
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon