1833. Maximum Ice Cream Bars Medium

@problem@discussion
#Array#Greedy#Sorting



1/**
2 * [1833] Maximum Ice Cream Bars
3 *
4 * It is a sweltering summer day, and a boy wants to buy some ice cream bars.
5 * 
6 * At the store, there are n ice cream bars. You are given an array costs of length n, where costs[i] is the price of the i^th ice cream bar in coins. The boy initially has coins coins to spend, and he wants to buy as many ice cream bars as possible. 
7 * 
8 * Return the maximum number of ice cream bars the boy can buy with coins coins.
9 * 
10 * Note: The boy can buy the ice cream bars in any order.
11 * 
12 *  
13 * Example 1:
14 * 
15 * 
16 * Input: costs = [1,3,2,4,1], coins = 7
17 * Output: 4
18 * Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
19 * 
20 * 
21 * Example 2:
22 * 
23 * 
24 * Input: costs = [10,6,8,7,7,8], coins = 5
25 * Output: 0
26 * Explanation: The boy cannot afford any of the ice cream bars.
27 * 
28 * 
29 * Example 3:
30 * 
31 * 
32 * Input: costs = [1,6,3,1,2,5], coins = 20
33 * Output: 6
34 * Explanation: The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.
35 * 
36 * 
37 *  
38 * Constraints:
39 * 
40 * 
41 * 	costs.length == n
42 * 	1 <= n <= 10^5
43 * 	1 <= costs[i] <= 10^5
44 * 	1 <= coins <= 10^8
45 * 
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/maximum-ice-cream-bars/
50// discuss: https://leetcode.com/problems/maximum-ice-cream-bars/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55    pub fn max_ice_cream(costs: Vec<i32>, coins: i32) -> i32 {
56        0
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_1833() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.