[LeetCode] How Many Apples Can You Put into the Basket

1196. How Many Apples Can You Put into the Basket

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int maxNumberOfApples(vector<int>& A) {
sort(begin(A), end(A));
for(int i = 0; i < A.size(); i++) {
if(i) A[i] += A[i-1];
if(A[i] > 5000) return i;
}
return A.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/14/PS/LeetCode/how-many-apples-can-you-put-into-the-basket/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.