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 ▌