Problem Statement
Coin Change
You have a handful of coin types, like a 1, a 5, and an 11. You can use each type as many times as you want. You are also given a target amount of money. Your job is to make that exact amount using the fewest coins possible, and return how many coins that takes. If there is no way to make the amount with the coins you have, return -1.
Signals to notice
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.
Trace it on coins=[1,5,11], amount=11
init: dp=[0,12,12,12,12,12,12,12,12,12,12,12] (12=infinity), dp[0]=0 i=1: coin1 -> dp[0]+1=1; dp[1]=1 i=2..4: only coin1 fits -> dp[2]=2, dp[3]=3, dp[4]=4 i=5: coin1->dp[4]+1=5, coin5->dp[0]+1=1; dp[5]=1 i=6: coin1->dp[5]+1=2, coin5->dp[1]+1=2; dp[6]=2 i=10: best via coin5->dp[5]+1=2; dp[10]=2 i=11: coin1->dp[10]+1=3, coin5->dp[6]+1=3, coin11->dp[0]+1=1; dp[11]=1 return dp[11]=1 (<=11, so not -1) -> answer 1
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.
Shape of the loop
dp = array of size amount+1, filled with amount+1 // "infinity"
dp[0] = 0
for i in 1..amount:
for coin in coins:
if coin <= i:
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount] if dp[amount] <= amount else -1Pseudocode only — the full worked solution lives in the Solution tab.
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.