Maximum Ice Cream Bars - Problem

It's a scorching hot summer day, and a young boy walks into an ice cream shop with his hard-earned coins jingling in his pocket. ๐Ÿฆ

The shop displays n different ice cream bars, each with its own price tag. You're given an array costs where costs[i] represents the price of the i-th ice cream bar in coins.

The boy has exactly coins coins to spend and wants to maximize his happiness by buying as many ice cream bars as possible. Since he's smart about spending, he can choose to buy the ice cream bars in any order that gives him the best deal.

Your task: Return the maximum number of ice cream bars the boy can buy with his available coins.

Special Requirement: You must solve this problem using counting sort for optimal performance.

Input & Output

example_1.py โ€” Basic Case
$ Input: costs = [1,3,2,4,1], coins = 7
โ€บ Output: 4
๐Ÿ’ก Note: The boy can buy ice creams with costs [1,1,2,3] for a total of 7 coins. He cannot buy the ice cream with cost 4 as it would exceed his budget.
example_2.py โ€” Insufficient Budget
$ Input: costs = [10,6,8,7,7,8], coins = 5
โ€บ Output: 0
๐Ÿ’ก Note: The boy cannot buy any ice cream bars because even the cheapest one costs 6 coins, which exceeds his budget of 5 coins.
example_3.py โ€” Buy All
$ Input: costs = [1,6,3,1,2,5], coins = 20
โ€บ Output: 6
๐Ÿ’ก Note: The boy can buy all ice cream bars since the total cost (1+6+3+1+2+5 = 18) is less than his budget of 20 coins.

Constraints

  • costs.length == n
  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค costs[i] โ‰ค 105
  • 1 โ‰ค coins โ‰ค 108
  • Must use counting sort for optimal solution

Visualization

Tap to expand
๐Ÿฆ Smart Ice Cream Shopping StrategyProblem: Buy maximum ice creams with limited budgetInput: costs = [1,3,2,4,1], coins = 7Step 1: CountPrice $1: 2 barsPrice $2: 1 barPrice $3: 1 barPrice $4: 1 barStep 2: Buy $1Buy 2 bars at $1Spent: $2, Bars: 2Remaining: $5Step 3: Buy $2Buy 1 bar at $2Spent: $4, Bars: 3Remaining: $3Step 4: Buy $3Buy 1 bar at $3Spent: $7, Bars: 4Remaining: $0Why Counting Sort?โ€ข O(n + k) time complexity vs O(n log n) for regular sortingโ€ข Perfect for bounded integer ranges (ice cream prices)โ€ข Meets the specific problem requirement๐ŸŽฏ Final Result: 4 Ice Cream Bars!Bought bars costing: $1, $1, $2, $3 = $7 total
Understanding the Visualization
1
Count Available Options
Use counting sort to organize ice creams by price, counting how many are available at each price point
2
Start from Cheapest
Begin purchasing from the $1 section, then $2, then $3, etc.
3
Buy Until Budget Exhausted
At each price level, buy as many as your remaining budget allows
4
Stop When Unaffordable
Once you can't afford the current price level, you're done (all higher prices will also be unaffordable)
Key Takeaway
๐ŸŽฏ Key Insight: Greedy approach works perfectly here - always buy the cheapest available items first to maximize quantity. Counting sort makes this optimal by avoiding comparison-based sorting overhead.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
41.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen