Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 322 Coin Change - Walkthrough

Classic unbounded example

Coin Change is Unbounded Knapsack in disguise. Given coins [1,3,4][1, 3, 4] and amount 66, find the minimum number of coins. Let dp[a]dp[a] = minimum coins for amount aa.

Base case: dp[0]=0dp[0]=0. Transition: dp[a]=min(dp[acoin]+1)dp[a] = \min(dp[a - coin] + 1) for each coin. dp[1]=1dp[1]=1, dp[2]=2dp[2]=2, dp[3]=1dp[3]=1, dp[4]=1dp[4]=1, dp[5]=2dp[5]=2, dp[6]=2dp[6]=2 (using coins 3+33+3). This is "minimum count" rather than "maximum value", but the structure is the same. Unbounded items, capacity constraint, optimal selection.