Skip to main content
Back to problems
#1196
Easy Algorithms

How many apples can you put into the basket

Array Greedy Sorting
67.0% acceptance
Mar 31, 2026
230
17
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 ith apple, return the maximum number of apples you can put in the basket.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn max_number_of_apples(mut weight: Vec<i32>) -> i32 {
    weight.sort_unstable();
    let mut total = 0;
    for (i, &w) in weight.iter().enumerate() {
      total += w;
      if total > 5000 {
        return i as i32;
      }
    }
    weight.len() as i32
  }
}