Optimal Item Selection

You are given N items and a container with a maximum weight capacity of W. Each item i has a weight wi and a value vi .

Your task is to find the maximum total value of items you can select such that their total weight does not exceed the container's capacity W.

Example 1:

Input: n = 3, W = 8 , items = [(3,30), (4,50), (5,60)]

Output: 90

Explanation:

Take items 1 and 2:

weight = 3 + 4 = 7 ≤ 8
value = 30 + 50 = 80

But better:

items 2 + 3 → weight = 9 
items 1 + 3 → weight = 8  value = 90 

Example 2:

Input: n = 2, W = 5 , items = [(6,10), (7,20)]

Output: 0

Explanation : No item fits.

Now Your Turn!

Pick the correct output for the given input

Input: n = 4, W = 10 , items = [[3,5], [4,6], [5,7], [6,10]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ N ≤ 100
  • 1 ≤ W ≤ 109
  • 1 ≤ w[i] ≤ W
  • 1 ≤ v[i] ≤ 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maxValue(int n, int w, vector<vector<int>>& items) {
// Write your code here
return 0;
}
};
Test Case

Input:

W
Items