bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1196. How Many Apples Can You Put into the Basket

1196. How Many Apples Can You Put into the Basket

Leetcode Solutions

Approach: Sort

  1. Sort the array of apple weights in ascending order.
  2. Initialize a variable apples to count the number of apples in the basket, starting at 0.
  3. Initialize a variable units to track the current total weight of the basket, starting at 0.
  4. Iterate through the sorted array of weights: a. If adding the current apple's weight to units does not exceed 5000, add the apple to the basket. b. Increment apples by 1. c. Increment units by the weight of the current apple.
  5. Continue this process until adding another apple would exceed the weight limit or there are no more apples left.
  6. Return the value of apples.
UML Thumbnail

Approach: Min-Heap

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...