mediumDynamic ProgrammingDynamic Programming

Unbounded Knapsack

mediumTime: O(n * W)Space: O(W)

Recognize the pattern

maximize value with unlimited supplyeach item can be used many timescapacity constraint

Brute force idea

The naive version of Unbounded Knapsack sounds like this: Try all possible quantities of each item — exponential without memoization. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

Better approach

A calmer way to see Unbounded Knapsack is this: 1D DP: dp[w] = max value achievable with capacity w. For each capacity from 1 to W, try every item: dp[w] = max(dp[w], dp[w-weight[i]] + value[i]). The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Unbounded Knapsack is this: Unlike 0/1 knapsack, processing left-to-right in the 1D array ALLOWS reuse — dp[w-weight[i]] may already include item i, and that's correct for unbounded. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

One easy way to drift off course in Unbounded Knapsack is this: Processing right-to-left like 0/1 knapsack — that prevents reuse. For unbounded, process LEFT-to-RIGHT so items can be picked multiple times. The fix is usually to return to the meaning of each move, not just the steps themselves.

Dynamic Programming Pattern