Quick revision
The short answer
We fill an array dp where dp[a] is the fewest coins that make amount a: dp[0] = 0, every other entry starts at a sentinel of amount + 1, and dp[a] = min over coins c <= a of dp[a - c] + 1. After filling amounts from 1 up to the target, we return -1 if dp[amount] is still the sentinel. It runs in O(amount x coins) time and O(amount) space, and we use DP rather than greedy because greedy fails for coin sets such as {1, 3, 4} with amount 6.
What to remember
In this answer, we saw why greedy fails for coin sets like {1, 3, 4} and why dynamic programming fixes it. We defined dp[a] as the fewest coins making a, used dp[a] = min over coins c <= a of dp[a - c] + 1 with dp[0] = 0, filled the table bottom-up, and returned -1 when dp[amount] stayed at the sentinel. Choosing amount + 1 as the sentinel avoids the overflow of Integer.MAX_VALUE + 1. The solution runs in O(amount x coins) time and O(amount) space.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We fill an array dp where dp[a] is the fewest coins that make amount a: dp[0] = 0, every other entry starts at a sentinel of amount + 1, and dp[a] = min over coins c <= a of dp[a - c] + 1. After filling amounts from 1 up to the target, we return -1 if dp[amount] is still the sentinel. It runs in O(amount x coins) time and O(amount) space, and we use DP rather than greedy because greedy fails for coin sets such as {1, 3, 4} with amount 6.
Why this question is asked
Coin change is the standard first dynamic programming problem in interviews, because it is small enough to code in ten minutes but shows the whole DP method: define what a subproblem means, write the recurrence, choose the base case, decide the order of filling, and handle impossible cases. It also has a trap, the greedy solution, that works for US coins and fails for others. In this answer, we will see why greedy is not enough, derive the recurrence, write the bottom-up solution, trace it on a small example, look at the sentinel choice and the overflow it avoids, and finish with the complexity.
1Why Greedy Is Not Enough
The obvious idea is greedy: always take the largest coin that fits. For {1, 5, 10, 25} and 63 cents, greedy takes 25, 25, 10, 1, 1, 1, six coins, and that happens to be optimal. Coin systems where greedy is always optimal are called canonical, and US coins are one of them.
But many coin sets are not canonical. With coins {1, 3, 4} and amount 6, greedy takes 4, then 1, then 1, three coins, while 3 + 3 uses only two. Greedy commits to the big coin and can never undo that choice. Since the method should work for any coin array the caller passes, we need an approach that considers every choice, which is what dynamic programming does efficiently.
| Coins | Amount | Greedy | Optimal |
|---|---|---|---|
| {1, 5, 10, 25} | 63 | 25+25+10+1+1+1 = 6 coins | 6 coins |
| {1, 3, 4} | 6 | 4+1+1 = 3 coins | 3+3 = 2 coins |
| {2, 5} | 3 | 2, then stuck | impossible: -1 |
2Deriving the Recurrence
Dynamic programming, solving a problem by combining answers to smaller versions of the same problem and storing them so each is computed once, starts with the question: what is the last coin? If the best way to make amount a ends with coin c, then the rest is the best way to make a - c, because if there were a better way to make a - c we could use it and improve a. So dp[a] = 1 + dp[a - c] for the best choice of c, which gives the recurrence dp[a] = min over coins c with c <= a of dp[a - c] + 1.
The base case is dp[0] = 0, since zero coins make amount zero. For amounts that cannot be made, we need a value meaning 'impossible' that loses every min comparison. Since no amount a needs more than a coins when 1 is available, and the answer can never exceed amount, the value amount + 1 is larger than any real answer.
3Writing the Bottom-Up Solution
Bottom-up means we fill dp from small amounts to large ones, so every dp[a - c] we read is already final. The outer loop goes over amounts and the inner loop over coins.
Notice that the code never checks dp[a - c] for 'impossible' explicitly. If dp[a - c] is the sentinel, then dp[a - c] + 1 is amount + 2, which never beats the current dp[a], so unreachable amounts stay at the sentinel automatically.
4Tracing It on {1, 3, 4} and Amount 6
It helps to watch the table fill. For each amount we try every coin and keep the best. At a = 3, coin 3 gives dp[0] + 1 = 1. At a = 4, coin 4 gives dp[0] + 1 = 1. At a = 6, coin 1 gives dp[5] + 1 = 3, coin 3 gives dp[3] + 1 = 2, and coin 4 gives dp[2] + 1 = 3, so the minimum is 2, which is the 3 + 3 solution greedy missed.
| Amount a | dp[a] | Best last coin |
|---|---|---|
| 0 | 0 | base case |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 1 or 4 (4+1) |
| 6 | 2 | 3 (3+3) |
5Why Not Integer.MAX_VALUE as the Sentinel?
Many people initialise the array with Integer.MAX_VALUE to mean infinity. The problem is the + 1 in the recurrence. Integer.MAX_VALUE + 1 wraps around to Integer.MIN_VALUE, a large negative number, which wins every min comparison, so an impossible amount suddenly looks like the best answer.
The fix is either to use amount + 1 as the sentinel, as we did, or to skip a coin when dp[a - c] is MAX_VALUE before adding. The amount + 1 version is shorter and has no special case.
6Complexity and Variations
The outer loop runs amount times and the inner loop runs once per coin, so the time is O(amount x coins). For an amount of 10,000 and 4 coins that is 40,000 steps, which is instant. The array has amount + 1 ints, so space is O(amount). Notice that this is pseudo-polynomial: it grows with the numeric value of the amount, not with the size of the input, so an amount of 10 billion would be infeasible with a plain array.
A top-down version with recursion and memoization computes the same values but only for amounts reachable from the target, at the cost of recursion depth, which can overflow the stack for large amounts. The related question, how many ways make the amount, uses dp[a] += dp[a - c] with the coins in the outer loop, so that each combination is counted once regardless of order.
7Follow-up questions
How do you return the actual coins, not just the count?
Keep a second array last[a] that records the coin chosen when dp[a] improves. After filling, start at amount, append last[amount], subtract it, and repeat until zero. That adds O(amount) space and O(answer) time to rebuild.
Does the order of the loops matter here?
For the minimum count, no: amounts outer and coins inner, or coins outer and amounts inner, both give correct answers because every dp[a - c] is final when read in either order with ascending amounts. For counting combinations it matters, because coins outer counts each combination once and amounts outer counts permutations.
When would you use the top-down approach?
When the target is large but only a few amounts are reachable, for example coins {1000, 2500} and a large target, memoization with a HashMap only visits the reachable amounts. For dense problems the bottom-up array is faster and has no recursion depth risk.
8Conclusion
In this answer, we saw why greedy fails for coin sets like {1, 3, 4} and why dynamic programming fixes it. We defined dp[a] as the fewest coins making a, used dp[a] = min over coins c <= a of dp[a - c] + 1 with dp[0] = 0, filled the table bottom-up, and returned -1 when dp[amount] stayed at the sentinel. Choosing amount + 1 as the sentinel avoids the overflow of Integer.MAX_VALUE + 1. The solution runs in O(amount x coins) time and O(amount) space.
Open with
I define dp of a as the minimum number of coins that make amount a. The recurrence comes from thinking about the last coin: if the best solution for a ends with coin c, the rest must be the best solution for a minus c, so dp of a is the minimum over all coins c no larger than a of dp of a minus c, plus one.
Then, if they want more
The base case is dp of zero equals zero. I fill an int array of size amount plus one, initialised to amount plus one as a sentinel meaning impossible, because no real answer can be larger than the amount. Then I loop a from one to amount, and for each coin that fits I take the minimum.
At the end, if dp of amount is still the sentinel, I return minus one. That is O(amount times number of coins) time and O(amount) space. I deliberately avoid Integer.MAX_VALUE as the sentinel, because adding one overflows to a negative number and an impossible amount would win the minimum.
I would also mention why we need DP at all: greedy works for US coins, but for coins one, three and four with amount six, greedy picks four, one, one, three coins, while three plus three needs only two.
If the interviewer wants the coins themselves, I keep an array of the last coin chosen and walk back from the amount.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a Java method for the coin change problem: given coins int[] {1, 5, 10, 25} and an...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/6661ac6340/