How Many Apples Can You Put into the Basket - Problem

You're helping at a farmers market and have a basket that can carry up to 5000 units of weight. In front of you are several apples, each with different weights, and you want to maximize the number of apples you can fit in your basket.

Given an integer array weight where weight[i] represents the weight of the i-th apple, return the maximum number of apples you can put in the basket without exceeding the weight limit.

Goal: Maximize the count of apples, not the total weight.

Example: If you have apples with weights [100, 200, 150, 1000] and your basket capacity is 5000, you should pick the lighter apples first: [100, 200, 150] gives you 3 apples with total weight 450, which is better than picking the heavy apple [1000] which only gives you 1 apple.

Input & Output

example_1.py โ€” Basic Case
$ Input: [100, 200, 150, 1000]
โ€บ Output: 3
๐Ÿ’ก Note: We can pick apples with weights 100, 200, and 150. Total weight = 450 โ‰ค 5000, and we get 3 apples. We cannot include the 1000-weight apple as it would make the total 1450, which is still โ‰ค 5000, but picking the three lighter apples gives us more apples total.
example_2.py โ€” All Fit
$ Input: [900, 950, 800, 1000, 700, 800]
โ€บ Output: 5
๐Ÿ’ก Note: Sorted: [700, 800, 800, 900, 950, 1000]. We can pick 700+800+800+900+950 = 4150 โ‰ค 5000, giving us 5 apples. Adding the last apple (1000) would make it 5150 > 5000.
example_3.py โ€” Single Heavy Apple
$ Input: [4000, 3000, 1500]
โ€บ Output: 2
๐Ÿ’ก Note: Sorted: [1500, 3000, 4000]. We can pick 1500+3000 = 4500 โ‰ค 5000, giving us 2 apples. We cannot add the 4000-weight apple as it would exceed the limit.

Constraints

  • 1 โ‰ค weight.length โ‰ค 104
  • 1 โ‰ค weight[i] โ‰ค 104
  • Basket capacity is always 5000

Visualization

Tap to expand
๐Ÿงบ Apple Basket Problem: Greedy Strategy๐Ÿ“ฆ Basket Capacity: 5000 unitsGoal: Maximize NUMBER of apples (not total weight)Available Apples:100๐ŸŽ200๐ŸŽ150๐ŸŽ1000๐ŸŽSort by weight (lightest first)After Sorting & Greedy Selection:100โœ“ Pick150โœ“ Pick200โœ“ Pick1000โœ— Skip๐ŸŽฏ Result: 3 apples (100 + 150 + 200 = 450 โ‰ค 5000)Greedy choice always works: lighter apples = more apples!
Understanding the Visualization
1
Sort by Weight
Arrange all apples from lightest to heaviest - this ensures we consider the most 'efficient' choices first
2
Greedy Selection
Pick apples one by one starting from the lightest, as long as they fit in our basket
3
Stop at Limit
As soon as adding the next apple would exceed our capacity, stop and return the count
Key Takeaway
๐ŸŽฏ Key Insight: For maximizing count with weight constraints, greedy selection of lightest items first is always optimal. The intuition is simple: lighter items leave more room for additional items.
Asked in
Amazon 45 Google 32 Microsoft 28 Apple 15
28.5K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen