How Many Apples Can You Put into the Basket - Problem
You have some apples and a basket that can carry up to 5000 units of weight.
Given an integer array weight where weight[i] is the weight of the i-th apple, return the maximum number of apples you can put in the basket.
Input & Output
Example 1 — Basic Case
$
Input:
weight = [100,200,150,500]
›
Output:
4
💡 Note:
All apples can fit: 100 + 200 + 150 + 500 = 950 ≤ 5000, so we can take all 4 apples
Example 2 — Weight Limit Exceeded
$
Input:
weight = [900,950,800,1000,700,800]
›
Output:
5
💡 Note:
After sorting: [700,800,800,900,950,1000]. We can take 700+800+800+900+950=4150 ≤ 5000, but adding 1000 would exceed limit
Example 3 — Single Heavy Apple
$
Input:
weight = [5001]
›
Output:
0
💡 Note:
The single apple weighs 5001 > 5000, so we cannot take any apples
Constraints
- 1 ≤ weight.length ≤ 103
- 1 ≤ weight[i] ≤ 104
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code