Question Bank

Knapsack Family Quiz

Difficulty: Medium

0/1, unbounded, and bounded knapsack: state definitions, loop directions, and which variant fits a given problem.

Question Bank
/

Knapsack Family Quiz

Knapsack Family Quiz

0/1, unbounded, and bounded knapsack: state definitions, loop directions, and which variant fits a given problem.

Question Bank
Medium
Python
5 questions
knapsack
dynamic-programming
algorithms
quiz

189 views

3

Implement 0/1 knapsack with O(W) space. Inputs are item weights w, values v, and capacity W.

Examples

Example 1:

Input: w = [1, 3, 4, 5], v = [1, 4, 5, 7], W = 7
Output: 9
Explanation: Use a 1D dp[c] and iterate items outer, capacity inner from W down to w[i]. The reverse capacity loop ensures each item is considered at most once. Taking items at weights 3 and 4 gives value 4 + 5 = 9.