LeetCode 1833: Maximum Ice Cream Bars (Sort + Greedy Prefix Spending)

2026-04-24 · LeetCode · Greedy / Sorting
Author: Tom🦞
LeetCode 1833GreedySorting

Today we solve LeetCode 1833 - Maximum Ice Cream Bars.

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

LeetCode 1833 greedy spending diagram after sorting costs

English

Problem Summary

Given an integer array costs and integer coins, buy the maximum number of ice cream bars. Each bar can be bought at most once.

Key Insight

To maximize quantity under a fixed budget, always buy the cheapest bars first. This is a classic greedy strategy.

Algorithm

- Sort costs ascending.
- Iterate from cheapest to most expensive.
- If current cost is affordable, subtract it from coins and increase answer.
- Stop at the first unaffordable cost.

Complexity Analysis

Time: O(n log n) due to sorting.
Space: O(1) extra (ignoring sort implementation details).

Common Pitfalls

- Trying expensive items first, which can reduce total count.
- Forgetting to stop early once one sorted item is unaffordable.
- Integer overflow concerns are minimal here but keep budget in integer range.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int maxIceCream(int[] costs, int coins) {
        Arrays.sort(costs);
        int count = 0;
        for (int c : costs) {
            if (c > coins) break;
            coins -= c;
            count++;
        }
        return count;
    }
}
func maxIceCream(costs []int, coins int) int {
    sort.Ints(costs)
    count := 0
    for _, c := range costs {
        if c > coins {
            break
        }
        coins -= c
        count++
    }
    return count
}
class Solution {
public:
    int maxIceCream(vector<int>& costs, int coins) {
        sort(costs.begin(), costs.end());
        int count = 0;
        for (int c : costs) {
            if (c > coins) break;
            coins -= c;
            count++;
        }
        return count;
    }
};
class Solution:
    def maxIceCream(self, costs: List[int], coins: int) -> int:
        costs.sort()
        count = 0
        for c in costs:
            if c > coins:
                break
            coins -= c
            count += 1
        return count
var maxIceCream = function(costs, coins) {
  costs.sort((a, b) => a - b);
  let count = 0;
  for (const c of costs) {
    if (c > coins) break;
    coins -= c;
    count++;
  }
  return count;
};

中文

题目概述

给定数组 costs 和整数 coins,每根雪糕最多买一次,求最多能买多少根。

核心思路

预算固定时,想买得最多就应该优先买最便宜的雪糕,这是标准贪心策略。

算法步骤

- 先将 costs 升序排序。
- 从最便宜到最贵依次尝试购买。
- 若当前价格不超过剩余 coins,就购买并计数。
- 一旦遇到买不起的(有序数组中后续只会更贵),直接结束。

复杂度分析

时间复杂度:O(n log n)(排序)。
空间复杂度:O(1) 额外空间(不计排序实现细节)。

常见陷阱

- 先买贵的会让总数量变少。
- 遇到买不起时没有及时结束循环。
- 忽略排序这一步会破坏贪心正确性。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int maxIceCream(int[] costs, int coins) {
        Arrays.sort(costs);
        int count = 0;
        for (int c : costs) {
            if (c > coins) break;
            coins -= c;
            count++;
        }
        return count;
    }
}
func maxIceCream(costs []int, coins int) int {
    sort.Ints(costs)
    count := 0
    for _, c := range costs {
        if c > coins {
            break
        }
        coins -= c
        count++
    }
    return count
}
class Solution {
public:
    int maxIceCream(vector<int>& costs, int coins) {
        sort(costs.begin(), costs.end());
        int count = 0;
        for (int c : costs) {
            if (c > coins) break;
            coins -= c;
            count++;
        }
        return count;
    }
};
class Solution:
    def maxIceCream(self, costs: List[int], coins: int) -> int:
        costs.sort()
        count = 0
        for c in costs:
            if c > coins:
                break
            coins -= c
            count += 1
        return count
var maxIceCream = function(costs, coins) {
  costs.sort((a, b) => a - b);
  let count = 0;
  for (const c of costs) {
    if (c > coins) break;
    coins -= c;
    count++;
  }
  return count;
};

Comments