easy +10 pts

Maximum Ice Cream Bars

Buy as many ice cream bars as possible with your given coins.

You are given an array `costs` of integers where `costs[i]` is the price of the i-th ice cream bar and an integer `coins` representing the total amount of money you have. You want to buy as many ice cream bars as possible. Return the maximum number of ice cream bars you can buy with the given coins. Implement the function `max_ice_cream_bars(costs, coins)`: - `costs`: list of positive integers (prices of ice cream bars). - `coins`: non-negative integer (total money you have). - Returns an integer representing the maximum number of bars you can buy. You can only buy each bar at most once, and you do not need to spend all your coins.

Constraints

- 1 <= len(costs) <= 10^5 - 1 <= costs[i] <= 10^4 - 0 <= coins <= 10^9 - You can assume that the sum of costs is within the range of a 64-bit integer. Complexity: Your solution should run in O(n log n) time or better.

Example

>>> max_ice_cream_bars([1,3,2,4,1], 7)
4
>>> max_ice_cream_bars([10,6,8,7,7,8], 5)
0
>>> max_ice_cream_bars([1,6,3,1,2,5], 20)
6
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about which bars to buy first to maximize quantity.
Sorting the costs is a natural first step.
Keep accumulating the total cost and stop when you can't afford the next bar.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.