Skip to content
AI360Xpert
Beta
Difficulty: Medium1-D Dynamic Programming

Coin Change

Problem in Plain English

Given coin denominations with unlimited supply, find the smallest number of coins that sums to amount, or -1 when the amount cannot be formed.

Problem Statement

Given positive denominations in coins and a target amount, choose as many copies of each denomination as needed. Return the minimum number of coins whose total is exactly the target. Return -1 if no choice reaches it. Return zero for amount zero.

Coin order does not change a combination, and the input array remains unchanged.

Constraints

  • Coin count is between 1 and 12.
  • Every denomination is an integer from 1 through 231−12^{31}-1.
  • The target amount is an integer from 0 through 10410^4.

Examples

Example 1

Input
coins = [1, 2], amount = 3
Output
2

The sequences [1, 2] and [2, 1] use two coins, whereas [1, 1, 1] uses three. No denomination equals 3, so one coin cannot suffice.

Example 2

Input
coins = [2], amount = 3
Output
-1

Every total made from copies of 2 is even, so the odd target 3 is unreachable.

Example 3

Input
coins = [1], amount = 0
Output
0

The empty selection already totals zero. Adding any positive coin would exceed the target.

Example 4

Input
coins = [1, 2, 5], amount = 11
Output
3

The selection [5, 5, 1] reaches 11 with three coins. Two coins total at most 10, so three is minimal.

Example 5

Input
coins = [1, 3, 4], amount = 6
Output
2

Two copies of 3 reach 6. Taking the largest coin 4 first would require two further 1s, showing why greedy choice fails.

Intuition

Remove one coin from a solution and what remains is a smaller version of the same problem. With denominations [1, 2] and target 3, choosing 1 leaves remainder 2, while choosing 2 leaves remainder 1. Both continuations need one more coin, so the answer is two. Searching every coin order repeats these remainders; a table can instead solve each amount once. Largest-first greedy choices are unsafe: [1, 3, 4] and target 6 need two 3s, while taking 4 first needs three coins.

Approaches

Recursive Coin Search

Solution Details

Reveal Recursive Coin Search: intuition, complexity, and code

Hints

Hint 1
Consider every possible first coin rather than choosing the largest.
Hint 2
The answer for a remainder depends only on that remainder, not on the coin order used to reach it.
Hint 3
Seed amount zero with zero coins and fill smaller amounts before larger ones, carrying a separate unreachable value.

Edge Cases

  • A zero target needs zero coins for any denominations, even when all coins exceed the target.
  • A target smaller than every coin is impossible: [5, 2147483647] and target 3 return -1.
  • The odd target 3 with only coin 2 is unreachable, despite having a smaller affordable coin.
  • Having coin 1 guarantees reachability but does not guarantee greedy optimality: [1,3,4] and target 6 return 2.
  • An exact denomination needs one coin; repeated uses remain legal, such as [2] and target 4 returning 2.

Common Mistakes and Interview Tips

  • Largest-first greedy fails for [1,3,4] and target 6; explain why arbitrary denomination systems require exploring alternatives.
  • Adding one to an unguarded maximum-integer sentinel can overflow. Use A+1 and add only to reachable results.
  • Returning the sentinel directly exposes an impossible count instead of the required -1.
  • Filling amounts in descending order reads unfinished dependencies and can accidentally impose single-use semantics.
  • Interview tip: distinguish minimizing coin count from counting combinations, and explain the smaller-remainder invariant before coding.
  • The explicit stack prevents stack overflow but does not remove exponential repeated work; only the table approach shares results.

Key Takeaway

An unlimited-choice minimum can be expressed as one choice plus an optimal smaller capacity. Once positive choices make dependencies strictly smaller, increasing-capacity dynamic programming replaces a repeated search with one calculation per state.