Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Dynamic Programming

Unbounded Knapsack

O(n · W)

Same as 0/1 but iterate capacity forward so an item can be picked again within the same state.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn first0/1 KnapsackUnlocksBounded (Multiple) Knapsack
0%
mastery
0/7 solved
IdeaPracticeTemplatePitfalls

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.

  1. 1Brute force

    Recursion over every coin at every step.

  2. 2The observation

    Exponential recursion: dp over amounts.

  3. 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); seed dp[0]=0, everything else infinity.
  • Count ways (Coin Change II): dp[a] += dp[a - coin]; seed dp[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

Next · Foundation

Coin Change

dp[a] = min(dp[a - c] + 1); greedy fails for arbitrary coins.

0/7 solved
  • 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, rest INF; return -1 if still INF, 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)) so dp[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.

Resources

  • DP lecture notesErrichto · video
  • AtCoder Educational DP ContestAtCoder · sheet
  • Introduction to DPUSACO Guide · course
  • Dynamic Programming PatternsLeetCode discuss · article
  • Dynamic Programming playlistAditya Verma · video
  • Bitmask DPUSACO Guide · course
  • Digit DPCodeforces · blog
0/1 KnapsackLCS / Two-Sequence
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial