You are given an array weight
where each element represents the weight of an apple. You also have a basket that can carry at most 5000
units of weight. Your task is to determine the maximum number of apples you can put into the basket without exceeding the weight limit.
Key constraints:
5000
.At first glance, it might seem like we need to try all possible combinations of apples to find the largest group that fits within the weight limit—this would be an exhaustive, brute-force approach. However, since we want to maximize the number of apples (not the total weight), we should focus on picking as many light apples as possible.
The lighter the apple, the less it contributes to the total weight, allowing us to include more apples overall. This suggests that sorting the apples by weight and picking them in increasing order is likely the best strategy. This is a classic greedy approach, where we always make the optimal local choice (the lightest apple next) to build up to the global optimum.
Let's break down the steps to solve the problem efficiently:
weight
array in ascending order so that we can always consider the lightest apple first.
curr_weight
) and a counter (count
). Iterate through the sorted list, and for each apple:
5000
, include it (increment count
and update curr_weight
).5000
, stop—the basket can't hold any more apples.count
holds the maximum number of apples that fit.
We use sorting because it allows us to always pick the smallest available apple, and iterating through the sorted array ensures we never miss a better combination (since heavier apples would always use up more capacity per apple).
Let's use the input weight = [100, 200, 150, 1000]
:
[100, 150, 200, 1000]
curr_weight = 0
, count = 0
.
curr_weight = 100
, count = 1
curr_weight = 250
, count = 2
curr_weight = 450
, count = 3
curr_weight = 1450
, count = 4
4
.
If the next apple in the list had made curr_weight
exceed 5000
, we would have stopped before adding it.
class Solution:
def maxNumberOfApples(self, weight):
weight.sort()
curr_weight = 0
count = 0
for w in weight:
if curr_weight + w > 5000:
break
curr_weight += w
count += 1
return count
class Solution {
public:
int maxNumberOfApples(vector<int>& weight) {
sort(weight.begin(), weight.end());
int curr_weight = 0, count = 0;
for (int w : weight) {
if (curr_weight + w > 5000) break;
curr_weight += w;
count++;
}
return count;
}
};
class Solution {
public int maxNumberOfApples(int[] weight) {
Arrays.sort(weight);
int curr_weight = 0, count = 0;
for (int w : weight) {
if (curr_weight + w > 5000) break;
curr_weight += w;
count++;
}
return count;
}
}
var maxNumberOfApples = function(weight) {
weight.sort((a, b) => a - b);
let curr_weight = 0, count = 0;
for (let w of weight) {
if (curr_weight + w > 5000) break;
curr_weight += w;
count++;
}
return count;
};
Brute-force approach: If we tried every combination of apples, the time complexity would be exponential, specifically O(2n), as there are 2n possible subsets. This is infeasible for large inputs.
Optimized (Greedy) approach:
The problem asks us to maximize the number of apples in a basket without exceeding a weight limit. The key insight is that, to fit as many apples as possible, we should always pick the lightest apples first. This leads to a greedy algorithm: sort the weights, then add apples in order until the basket can't hold any more. This approach is simple, efficient, and leverages the problem's structure for an elegant solution.