workshop private

← all creations

Change

viz · created 2026-09-04

Greedy and the DP table racing the same coin-change instance on one shared number line — greedy stacking largest-first up top, the table filling left to right with each cell reaching back along an arrow to the sub-amount it came from, and a marker dropped on the first amount where the two answers diverge.

algorithmsinterview-prepcanvas

Coin Change (LC 322) answered two ways at once. Up top, greedy builds its stack the way everyone’s first instinct does: largest coin that fits, repeat. Below it, the one-dimensional DP table fills one cell per tick, each cell asking “one coin more than the cheapest sub-amount a single coin reaches back to” — and the reach is drawn, an arc from dp[a] to dp[a − c] for every coin that fits, the winning one bold in that coin’s colour. When the table is full, the arrows are walked back from the amount to zero and the coins they name are laid down as a second stack on the same number line, so the two answers sit one above the other in the same units.

The point of the piece is the default. On US denominations the two stacks agree, every time, on every amount — and the demo lets you sit in that agreement long enough to believe greedy is safe. Then switch to {1, 3, 4} and ask for 6: greedy takes 4 and pays two 1s while the table reaches through 3 + 3 and lands a coin lower. A marker drops on the first amount where greedy and the table disagree for whatever coin system is loaded, so “greedy is wrong here” stops being a footnote and becomes a cell you can point at in an interview. {1, 6, 10} for 24 is the dramatic one (greedy 6 coins, optimal 4); {3, 4} for 6 shows greedy stranding a remainder the coin system can cover; {2} for 3 shows the ∞ that becomes the −1.

Reuse

src/change.js is a framework-free ES module:

No rendering or timers in the module; the canvas demo is reference code.

Gotchas