mediumDynamic ProgrammingDynamic Programming

Coin Change

mediumTime: O(n * amount)Space: O(amount)

Signals to notice

minimum number to reach targetunlimited supply of choicesoptimal substructure

Brute force first

Try all combinations of coins recursively — exponential time. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

DP bottom-up: dp[amount] = min coins to make that amount, try each coin. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

What must stay true

dp[i] = 1 + min(dp[i - coin]) for each coin denomination. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Easy way to go wrong

Not initializing dp[0] = 0 — zero coins are needed to make amount 0. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Dynamic Programming Pattern