1833. Maximum Ice Cream Bars
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 23, 2024
Last updated : June 23, 2024
Related Topics : Array, Greedy, Sorting
Acceptance Rate : 73.798 %
int comp(const void* a, const void* b) {
return *((int*) a) - *((int*) b);
}
int maxIceCream(int* costs, int costsSize, int coins) {
qsort(costs, costsSize, sizeof(int), comp);
for (int i = 0; i < costsSize; i++) {
if (coins < costs[i])
return i;
coins -= costs[i];
}
return costsSize;
}
class Solution {
public int maxIceCream(int[] costs, int coins) {
Arrays.sort(costs);
for (int i = 0; i < costs.length; i++) {
if (coins < costs[i])
return i;
coins -= costs[i];
}
return costs.length;
}
}
class Solution:
def maxIceCream(self, costs: List[int], coins: int) -> int:
bars = 0
costs.sort(reverse=True)
while costs :
if coins < costs[-1] :
return bars
bars += 1
coins -= costs.pop()
return bars