Knapsack problem
Choosing which items, each with a weight and a value, to put in a knapsack of limited capacity so the total value is as high as possible.
Example
With capacity 4 and items of weight 1, 2 and 3 worth 5, 11 and 15, the best is to take the weight-1 and weight-3 items: value 20.
Test yourself: Knapsack problem
0-1 knapsack
It is called 0-1 knapsack because each item is taken 0 or ___ time.
No halves or copies: with 3 items there are 2³ = 8 combinations of taking or skipping.
0-1 knapsack
The knapsack state is [i, c]: the item and the free ___.
After taking the weight-3 item in capacity 4, c = 1 is left for the rest.
0-1 knapsack
With a single list, in the 0-1 knapsack the capacities are walked from ___ to smallest.
That way dp[c − w] still holds the previous row's value: with capacity 10, you go from 10 down to 0.
0/3
â–¶ Play