Skip to content
StackDecode
Dynamic programmingAnimated

Unbounded Knapsack

Like 0/1 knapsack but each item can be used unlimited times.

Time

O(n · W)

Extra space

O(W)

Use it for

Cutting and coin-style problems.

How it works

  1. Like 0/1 knapsack, but every item can be used any number of times.
  2. best(c) = the best value with capacity c. best(0) = 0.
  3. For each capacity try every item that fits: best(c) = max over items of value + best(c − weight).
  4. Because the same item may be reused, a single row is enough. O(capacity × items).

Watch it run

Loading animation…