Skip to content

Latest commit

 

History

History
82 lines (61 loc) · 1.7 KB

_1833. Maximum Ice Cream Bars.md

File metadata and controls

82 lines (61 loc) · 1.7 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 23, 2024

Last updated : June 23, 2024


Related Topics : Array, Greedy, Sorting

Acceptance Rate : 73.798 %


Solutions

C

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;
}

Java

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;
    }
}

Python

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