544. How Many Apples Can You Put into the Basket

You have a collection of apples and a basket that can hold up to 5000 units of weight.

Given an integer array weight, where weight[i] represents the weight of the ith apple, return the maximum number of apples that can be placed in the basket without exceeding the weight limit.

Example 1:

Input: weight = [200, 300, 400, 500, 600]

Output: 5

Explanation:

The sum of all apple weights is 200 + 300 + 400 + 500 + 600 = 2000, which is within the basket's limit of 5000. Hence, all 5 apples can fit.

Example 2:

Input: weight = [1000, 1200, 1500, 2000, 800]

Output: 4

Explanation:

The total weight of all apples is 6500, which exceeds 5000. The best choice is selecting 1000 + 1200 + 1500 + 800 = 4500, fitting 4 apples.

Now Your Turn!

Pick the correct output for the given input

Input: weight = [3000, 1500, 1000, 500, 200]

Still unsure what the problem is asking ?

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

Constraints:

  • 1 <= weight.length <= 103
  • 1 <= weight[i] <= 103

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int maxNumberOfApples(vector<int>& arr) {
// Your code goes here
}
};
Test Case

Input:

Weight