Maximum Load Of Two Bags

You are given an integer array weights and two integers w1 and w2 representing the maximum capacities of two bags.

Each item may be placed in at most one bag such that:

  • Bag 1 holds at most w1 total weight.
  • Bag 2 holds at most w2 total weight.

Return the maximum total load that can that the two bags can carry together.

Example 1:

Input: weights = [1,4,3,2], w1 = 5, w2 = 4

Output: 9

Explanation:

  • Bag 1: Place weights[2] = 3 and weights[3] = 2 as 3 + 2 = 5 <= w1
  • Bag 2: Place weights[1] = 4 as 4 <= w2
  • Total weight: 5 + 4 = 9

Example 2:

Input: weights = [3,6,4,8], w1 = 9, w2 = 7

Output: 15

Explanation:

  • Bag 1: Place weights[3] = 8 as 8 <= w1
  • Bag 2: Place weights[0] = 3 and weights[2] = 4 as 3 + 4 = 7 <= w2
  • Total weight: 8 + 7 = 15

Now Your Turn!

Pick the correct output for the given input

Input: weights = [5,7], w1 = 2, w2 = 3

Still unsure what the problem is asking ?

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

Constraints:

  • 1 <= weights.length <= 100
  • 1 <= weights[i] <= 100
  • 1 <= w1, w2 <= 300

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maximumLoadOfTwoBags(vector<int>& weights, int w1, int w2) {
}
};
 
Test Case

Input:

W1
W2
Weights