Unbounded Knapsack
O(n · W)
Same as 0/1 but iterate capacity forward so an item can be picked again within the same state.
0/7 solved
Recognise it
The signal
Items reusable any number of times: coin change, perfect squares, rod cutting; min count or number of ways to reach a total.
1Brute force
Recursion over every coin at every step.
2The observation
Exponential recursion: dp over amounts.
3Why it works
dp[a] = best/count for amount a using any multiset (or sequence) of items.
Reach for it when
- +Items are reusable unlimited times under a target/capacity — coins, rod pieces, squares.
- +'Fewest items to make T', 'number of ways to make T', 'can you make T'.
- +Same small-capacity setting as 0/1 knapsack.
Not when
- ×Each item usable only onceuse 0/1 knapsack (loop capacity backward)
- ×Amounts/weights are gigantic, so O(n·T) blows upuse Math / number theory
- ×You think greedy coin change is optimal: only for canonical coin systems, rarely guaranteeduse Trust the DP unless canonical is stated
Intuition
Unbounded knapsack is 0/1 knapsack with one rule relaxed: you may use each item as many times as you like. Coins are the poster child — you have unlimited pennies, nickels, dimes; make an amount with the fewest coins, or count the ways.
Read the full intuition
The recurrence barely changes, but one line flips and it matters enormously: in the 1-D array you now loop capacity forward. Forward means when you compute dp[w] you read dp[w - coin] that has already been updated with this same coin this round — which is exactly "use the coin again." (Backward = each item once = 0/1; forward = reuse allowed = unbounded. Memorise that single distinction and you own both patterns.)
Two classic shapes:
- Min count (Coin Change):
dp[a] = min(dp[a], dp[a - coin] + 1); seeddp[0]=0, everything elseinfinity. - Count ways (Coin Change II):
dp[a] += dp[a - coin]; seeddp[0]=1.
Key idea
Order of the two loops decides combinations vs permutations. Coins outer / amount inner ⇒ each coin considered once across all amounts ⇒ combinations (1+2 and 2+1 counted once). Amount outer / coin inner ⇒ permutations (counts orderings). Coin Change II wants combinations (coins outer); Combination Sum IV wants permutations (amount outer).
Why it costs what it costs
Time O(n·T). n item types × T target values, each O(1).
Space O(T). a single 1-D array over the target.
Practice
- Coin ChangeFoundationFoundationGoogle, NVIDIAMedium
- Minimizing CoinsApply it · Same at CSES scale.Apply itCSESEasy
- Coin Change IIOne change · Count combinations.One changeMedium
- Combination Sum IVTwists · Order matters (sequences).TwistsMedium
- Perfect SquaresBranches · Items are generated (squares).BranchesMedium
- Coin Combinations IICombine · Combinations at scale with mod.CombineCSESMedium
- Coin Combinations ICSESEasy
Pitfalls
Classic bugs
- !Looping capacity backward reverts to 0/1 (each coin once); unbounded loops forward.
- !Loop order decides the count: coins outer = combinations (Coin Change II), amount outer = permutations (Combination Sum IV).
- !Min-count: seed
dp[0]=0, restINF; return -1 if stillINF, don't add 1 to infinity. - !Counts overflow 32-bit (Combination Sum IV); use a wider type or the given mod.
- !Start the inner loop at
coin(range(coin, T+1)) sodp[a-coin]never goes negative.
Go deepervariants and techniques · resources
Variants and techniques
- Min countdp[a]=min(dp[a], dp[a-coin]+1), seed dp[0]=0. Coin Change, Perfect Squares.
- Count combinationsdp[a]+=dp[a-coin], coins outer, seed dp[0]=1. Coin Change II.
- Count permutationsdp[a]+=dp[a-coin], amount outer (orders matter). Combination Sum IV.
- Feasibilitydp[a] |= dp[a-coin]; ask dp[target]. Word Break is a string-flavoured cousin.