Coin Change
The problem
Section titled “The problem”You have coin denominations (each usable any number of times) and a target amount.
Return the smallest number of coins that sum to the amount, or -1 if no combination
works. An amount of 0 needs 0 coins.
coins = [1, 2, 5], amount = 11 -> 3 (5 + 5 + 1)coins = [2], amount = 3 -> -1Key insight
Section titled “Key insight”Greedy (“always take the biggest coin”) is wrong: with [1, 3, 4] and amount 6 it picks
4 + 1 + 1 (three coins) but 3 + 3 uses two. Brute-force recursion works but recomputes
the same amounts over and over.
The last coin you use is one of the denominations, and what is left is a smaller
instance of the same problem. So define dp[a] as the fewest coins to make a:
dp[a] = min(dp[a - coin] + 1) over every coin <= a, with dp[0] = 0. Fill a from
1 up to amount. Start every other cell at amount + 1 (an impossible “infinity”); if
dp[amount] is still that, return -1.
Show solutionHide solution
def coin_change(coins: list[int], amount: int) -> int: INF = amount + 1 # larger than any real answer dp = [0] + [INF] * amount # dp[a] = fewest coins that make amount a for a in range(1, amount + 1): for coin in coins: if coin <= a: dp[a] = min(dp[a], dp[a - coin] + 1) return dp[amount] if dp[amount] != INF else -1export function coinChange(coins: number[], amount: number): number { const INF = amount + 1; // larger than any real answer const dp: number[] = new Array(amount + 1).fill(INF); // dp[a] = fewest coins that make amount a dp[0] = 0; for (let a = 1; a <= amount; a++) { for (const coin of coins) { if (coin <= a) dp[a] = Math.min(dp[a], dp[a - coin] + 1); } } return dp[amount] === INF ? -1 : dp[amount];}import java.util.Arrays;
class Solution { public int coinChange(int[] coins, int amount) { int inf = amount + 1; // larger than any real answer int[] dp = new int[amount + 1]; // dp[a] = fewest coins that make amount a Arrays.fill(dp, inf); dp[0] = 0; for (int a = 1; a <= amount; a++) { for (int coin : coins) { if (coin <= a) dp[a] = Math.min(dp[a], dp[a - coin] + 1); } } return dp[amount] == inf ? -1 : dp[amount]; }}Complexity
Section titled “Complexity”- Time: O(amount × coins) — each of the
amountcells tries every coin once. - Space: O(amount) — one array of
amount + 1entries.
Watch out for
Section titled “Watch out for”- Don’t use greedy. It fails on
[1, 3, 4], amount 6. Say why if asked. - Amount 0 returns 0, not -1.
dp[0] = 0handles it with no special case. - Pick a safe “infinity”.
amount + 1can never be a real answer (the worst case isamountcoins of value 1), and adding 1 to it can’t overflow in Java. AvoidInteger.MAX_VALUEplus 1. - Unreachable is a normal result. Convert leftover infinity to
-1at the end.
Variations
Section titled “Variations”Coin Change II (LeetCode 518)
Section titled “Coin Change II (LeetCode 518)”Count the number of combinations that make the amount, not the fewest coins. The
trick: loop coins on the outside and amounts on the inside, with
dp[a] += dp[a - coin]. This order counts each combination once; swapping the loops
counts orderings (permutations) instead.
Perfect Squares (LeetCode 279)
Section titled “Perfect Squares (LeetCode 279)”Fewest perfect squares that sum to n. The trick: it is Coin Change where the
“coins” are 1, 4, 9, 16, ... up to n. Same recurrence, same bottom-up fill.
Minimum Cost For Tickets (LeetCode 983)
Section titled “Minimum Cost For Tickets (LeetCode 983)”Travel on given days and buy 1-, 7- or 30-day passes at minimum total cost. The trick:
dp[day] is the cheapest cost to cover travel up to that day; each pass type is a
“coin” that jumps back 1, 7 or 30 days.