All visualizationsDSA Patterns · 73 of 76
🧪 Your experiment — choose the coins, choose the amount, race the table against greedy
Up to 3 coin values (1–9, duplicates merged) and an amount (3–9). Challenge: build a set where grab-the-biggest LOSES.
coins =pay:
Coin Change Lab — race the table against greedy
Pick your own coins and amount, watch the DP ladder fill — and catch grab-the-biggest paying more than it had to.
💡
THE BIG IDEA
Fewest coins to pay an amount: the problem where the obvious plan — always grab the biggest coin — is sometimes WRONG, and dynamic programming earns its keep. Pick your own coins and amount; the table answers every smaller amount first, then your amount leans on those answers. The closing frame races the table against greedy on YOUR input. Can you build a coin set where greedy loses? One where it gets completely stuck? An amount nobody can pay?
Move your mouse over any line to see what it does. Hover a loop line and it plays through every repeat. Or press ▶ Play to watch the whole thing. On a phone, tap the arrows.
1coins = [1, 3, 4]
2amount = 6
3dp = [0] + [99] * amount
4for a in range(1, amount + 1):
5 for c in coins:
6 if c <= a and dp[a - c] + 1 < dp[a]:
7 dp[a] = dp[a - c] + 1
8print("fewest coins for", amount, "=", dp[amount])
Your coins are [1, 3, 4] and you must pay exactly 6 — with the FEWEST coins. The obvious plan is "keep grabbing the biggest coin". Sometimes that plan is wrong, and this table is how you beat it.
Dynamic programming answers every smaller amount first, so each answer can stand on the ones before it. Watch the ladder build — one rung per amount.
📦 Memory boxes
what the program is remembering right now
nothing remembered yet
🖥️ What the computer shows
the answers the program prints out
nothing yet
1/30
UP NEXT IN DSA PATTERNS
Sliding Window Lab — run it on YOUR numbers
Type any four numbers and a target, then watch the real algorithm hunt for the shortest window through them.
Binary Search Lab — hunt for YOUR numberSliding Window Lab — run it on YOUR numbers