BeginnerCoder

Lesson 20 — Coin change (DP)

Dynamic programming: small answers build big ones

Dynamic programming (DP) solves a big problem by first solving all the small versions and storing them in a table. For "fewest coins to make an amount", dp[a] = fewest coins for amount a. To fill dp[a], try every coin c: use one c, plus the best way to make a − c, which you already know.

best[t] = min(best[t], best[t - step] + 1)   # use one more step of this size

float("inf") (infinity) means "not possible yet".

Why it matters: DP solves route planning, text diffing and spell-check suggestions that brute force could never finish.

Want it another way?
Task

Write the dp update line. Expected: 3 (5 + 5 + 1), then 2 (3 + 3).

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Algorithms Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.