Dynamic programming

O(a · k)·O(a) space
Whole table

Drag to pan; pinch or Ctrl+scroll to zoom, which stops at the whole table; arrow keys pan once the picture has focus. The headers stay put while you pan.

amount 0, not yet filledamount 1, not yet filledamount 2, not yet filledamount 3, not yet filledamount 4, not yet filledamount 5, not yet filledamount 6, not yet filledamount 00amount 11amount 22amount 33amount 44amount 55amount 66the fewest coinscoins→ amount↓ coins

The table has 1 row and 7 columns, and nothing has been worked out yet. The coins are 1, 3 and 4, and each one is a different distance to reach back.

The table has 1 row and 7 columns, and nothing has been worked out yet. The coins are 1, 3 and 4, and each one is a different distance to reach back.
Answer so far—

C(a) = 1 + min over coins c of C(a − c)

  1. 1no coins are needed to make nothing, so that cell is 0
  2. 2try each coin that is not larger than this amount, reaching back to the amount it leaves
  3. 3a coin larger than the amount cannot be the last one, so skip it
  4. 4write one more than the best of them into the cell, remembering which coin it was
  5. 5walk back, subtracting the coin each cell remembered
  6. 6stop: the last cell is the fewest coins, and the walk back names them
Cells filled
0
Cells read
0
Values written
0
Coins too large
0

Complexity

BestO(a · k)
AverageO(a · k)
WorstO(a · k)
Extra spaceO(a)
amount × coins at n=7 predicts 21; this run took 0.

Coin change

To make an amount with the fewest coins, consider every coin you might hand over last. If you hand over a 5, you still have to make the amount minus five, and that answer is already in the table to the left. So the cell for an amount is one more than the smallest of the cells reachable by subtracting a single coin. The base case is zero, which needs no coins at all.

Its recurrence

C(a) = 1 + min over coins c of C(a − c)

When it is the right choice

Any "fewest pieces" question where the pieces can be reused: making change, cutting a length of pipe into fixed sizes, hitting a score with a fixed set of moves. Note that this is not the same problem as the greedy one — always taking the largest coin that fits works for ordinary currency and fails for sets like 1, 3 and 4, where making 6 greedily takes three coins and the table finds two. That failure is the reason this page exists rather than a greedy one.

How it works

Worth noticing

Cost

Best
O(a · k)
Average
O(a · k)
Worst
O(a · k)
Extra space
O(a)
Table
one dimension
Accepts
2 to 6 coins, each from 1 to 50; amount from 1 to 32

The limits are pedagogical rather than technical: this tool keeps the whole table in every recorded step, so the shape of the table sets the size of the trace directly, and a run has to stay short enough to sit and watch. An input that would take more than three thousand steps on this algorithm is refused before a single step is generated, from a formula in the input sizes rather than by running it and finding out.

An unfilled cell is drawn differently from a cell holding a zero — no fill, a dashed outline, and no number at all — because early in a run that difference is most of the picture.

Worth comparing

All dynamic programming algorithms