Spot the bug: one-row 0/1 knapsack
Standard · Find the bug · about 8 min
This one-row version of 0/1 knapsack should use each item at most once, but it returns too much for some inputs. Click the line with the bug, then fix it.
Example 1
Input:
[[1],[15],3]
Output:
15
The buggy code returns 45: item 0 is taken three times.
Constraints
- 1 ≤ weights.length ≤ 200
- 1 ≤ weights[i] ≤ 200
- 0 ≤ capacity ≤ 200
Learn the idea first: 0/1 and unbounded knapsack, a visual lesson.
More 0/1 and unbounded knapsack problems
- Fill the knapsack table Warm-up
- Take or skip? Warm-up
- 0/1 knapsack Standard
- Partition equal subset sum Standard
- Unbounded knapsack Standard
- Target sum Challenge