Description
It is a sweltering summer day, and a boy wants to buy some ice cream bars. At the store, there are n ice cream bars at various costs. The boy has coins in total. Return the maximum number of ice cream bars he can buy.
Examples
Input:
costs = [1,3,2,4,1], coins = 7Output:
4Explanation:
Buy bars at costs [1,1,2,3] = 7.
Input:
costs = [7,3,3,10,4], coins = 10Output:
3Explanation:
To maximize ice cream bars, buy the cheapest ones first. Sort costs: [3,3,4,7,10]. Buy bars at costs [3,3,4] = 10 coins exactly. Cannot afford the next cheapest (cost 7) as it would exceed the budget.
Input:
costs = [5,8,2,9,1,6], coins = 8Output:
3Explanation:
Sort costs in ascending order: [1,2,5,6,8,9]. With 8 coins, buy bars at costs [1,2,5] = 8 coins total. The next cheapest bar costs 6, but 8 + 6 = 14 exceeds our budget of 8 coins.
Constraints
- •
costs.length == n - •
1 ≤ n ≤ 10^5