Fill the knapsack table
Warm-up · Trace the algorithm · about 5 min
Items A, B and C weigh 2, 3 and 4 and are worth 3, 4 and 6. The bag holds weight 5. Follow the 0/1 table and answer each question before the step plays.
Example 1
Input:
{"weights":[2,3,4],"values":[3,4,6],"capacity":5}
Output:
7
Constraints
- The trace uses the items shown.
- Each item is taken at most once.
Learn the idea first: 0/1 and unbounded knapsack, a visual lesson.
More 0/1 and unbounded knapsack problems
- Take or skip? Warm-up
- 0/1 knapsack Standard
- Partition equal subset sum Standard
- Unbounded knapsack Standard
- Spot the bug: one-row 0/1 knapsack Standard
- Target sum Challenge