← Lessons

quiz vs the machine

Gold1360

Algorithms

The Unbounded Knapsack

Maximize value under a capacity when each item may be chosen any number of times.

4 min read · core · beat Gold to climb

Unlimited copies

Unbounded knapsack is like the zero one version, but each item type is available in unlimited supply. Coin change and rod cutting are common instances. The state can be a single dimension dp at w, the best value achievable at capacity w.

The recurrence

For capacity w, try every item that fits and take the best:

  • For each item, consider its value plus dp at w minus its weight.
  • Keep the maximum across all items.

Because the same item may be reused, the take branch reads the current capacity table, not a previous item row. This is the precise difference from the zero one case.

Iteration order

Fill capacities from small to large. When you reach dp at w, smaller entries are final, so reusing an item simply layers another copy onto an already optimal smaller solution.

Key idea

Unbounded knapsack reuses items freely by reading the current capacity table and sweeping capacities upward, so each entry can stack additional copies of any item onto smaller optimal answers.

Check yourself

Answer to earn rating on the learn ladder.

1. How does unbounded knapsack differ from the zero one version?

2. In what order are capacities filled for the one dimensional form?