↓Skip to main content
  1. LeetCode/

LeetCode 1833: Maximum Ice Cream Bars

·2 mins· ·
LeetCode Medium Greedy Sorting Counting-Sort
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-06-21

  • Submissions: Accepted ×2 (sorting, then counting sort)

Approach
#

To buy the most, buy the cheapest first (exchange argument: in any purchase, swapping an expensive bar for an unbought cheaper one keeps the count and does not raise the cost).

The problem asks for counting sort: prices are at most 10^5, so count each price and walk upward, buying min(cnt, coins // price) at each price. Once the coins cannot cover the current price, nothing pricier is affordable either, so stop. This is Greedy Template 1.

My Solution
#

The first version sorted and bought one at a time; the second switched to counting sort as required:

class Solution:
    def maxIceCream(self, costs: List[int], coins: int) -> int:
        cnt = [0] * (max(costs) + 1)
        for c in costs:
            cnt[c] += 1
        bought = 0
        for price in range(1, len(cnt)):
            if price > coins:
                break
            take = min(cnt[price], coins // price)
            coins -= take * price
            bought += take
        return bought

Complexity
#

Time O(n + M) with M the highest price, space O(M).

Mistakes to Avoid
#

  • You may not afford every bar at one price; take min(cnt, coins // price).

Interview Explanation
#

Buying cheapest first always maximizes the count. Since counting sort is required, I count each price and, from low to high, buy as many as I can at each price, stopping when the coins cannot cover the current price. Time O(n + M).

Related

LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3961: Maximize Sum of Device Ratings
·2 mins
LeetCode Medium Greedy Sorting Matrix
LeetCode 3961 note: every device but one donates its smallest unit to the same device; that device’s rating becomes the global minimum, so pick the one with the smallest second-smallest value