Why it matters

Coin change is classic DP. Understanding shapes unbounded knapsack.

Advertisement

The architecture

dp[a] = min coins for amount a.

Or dp[a] = number of ways.

Iterate over amounts + coins.

Coin change DPAmounts 0..NtargetFor each coinextend combinationsdp[a] updatemin or countOrder of loops differs for min vs count; watch overcounting
Coin change.
Advertisement

How it works end to end

Min coins: dp[a] = min(dp[a], dp[a-c] + 1).

Ways: dp[a] += dp[a-c] with coin outer loop.

Unbounded (repeats OK).