Return the fewest coins needed to make the amount, or -1 if it can't be made. You have unlimited coins of each denomination.
Before you code
Greedy (largest coin first) fails for some coin sets. The table over amounts tries every coin as the last one.
Hints
dp[0] = 0; all other amounts start at infinity.- For each amount from 1 upward,
dp[a] = min(dp[a - c] + 1)over coins c ≤ a. - Return -1 if
dp[amount]is still infinity.
Production & complexity notes
When solving Coin change on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Knapsack and subsets to reinforce the underlying pattern.
What it practices
The unbounded knapsack, from Knapsack and subsets.