跳过正文
  1. leetcode 题解/

1833_雪糕的最大数量

·44 字·1 分钟

类型:数组

    1. 雪糕的最大数量 💛

    https://leetcode-cn.com/problems/maximum-ice-cream-bars/

    ❓ 数组 costs 表示每支雪糕的价格,一共有现金 coins,问能买几个雪糕。

    💡 排序

    class Solution:
        def maxIceCream(self, costs: List[int], coins: int) -> int:
            costs.sort()
            count = 0
            for c in costs:
                if coins < c:
                    return count
                coins -= c
                count += 1
            return count

    时间复杂度:O(NlogN),空间复杂度:O(1)