#518

Coin Change II

Medium Unbounded knapsack (count combinations)

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

?The problem

Count the combinations of coins (unlimited supply) that add up to amount. Order doesn't matter.

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

◎Understanding

Really asking: how many combinations make the amount?

Why this works: loop coins outside, amounts inside, so each combination is counted once (order doesn't matter).

Example: amount 5, coins [1, 2, 5]

5, 2+2+1, 2+1+1+1, 1×5 → 4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Swapping the loops, which counts 1+2 and 2+1 as different (that's a different problem).

Solve on LeetCode ↗
Complexity
  • Time: O(amount · coins).
  • Space: O(amount).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon