mediumDynamic ProgrammingDynamic Programming

Coin Change

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

Recognize the pattern

minimum number to reach targetunlimited supply of choicesoptimal substructure

Brute force idea

If you approach Coin Change in the most literal way possible, you get this: 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.

Better approach

The deeper shift in Coin Change is this: 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.

Key invariant

At the center of Coin Change is one steady idea: 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.

Watch out for

A common way to get lost in Coin Change is this: 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