Talentd
FleetCode

1196. How Many Apples Can You Put into the Basket

Easy67.0% acceptance1 companyPremium elsewhere, free here

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.

Example 1:

Input: weight = [100,200,150,1000]
Output: 4
Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.

Example 2:

Input: weight = [900,950,800,1000,700,800]
Output: 5
Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.

Constraints:

  • 1 <= weight.length <= 103
  • 1 <= weight[i] <= 103

Asked at 1 company

Hints open one at a time

Hint 1
What if you think in a greedy approach?
Hint 2
The best apple to take in one step is the one with the smallest weight.
Hint 3
Sort the array and take apples with smaller weight first.
5:33
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[100,200,150,1000]
Input
[900,950,800,1000,700,800]
Your code is saved on this device.Solution