Skip to content

Commit 2da2a91

Browse files
committed
Create 1833-maximum-ice-cream-bars.rs
1 parent 7ce170e commit 2da2a91

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

1833-maximum-ice-cream-bars.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub fn max_ice_cream(costs: Vec<i32>, coins: i32) -> i32 {
2+
let mut sorted_costs = costs.clone();
3+
sorted_costs.sort();
4+
for i in 1..sorted_costs.len() {
5+
sorted_costs[i] += sorted_costs[i-1];
6+
}
7+
for (i, c) in sorted_costs.iter().enumerate() {
8+
if coins < *c { return i as i32 }
9+
}
10+
sorted_costs.len() as i32
11+
}
12+
13+
fn main() {
14+
let costs = vec![1,6,3,1,2,5];
15+
let coins = 20;
16+
println!("{:?}", max_ice_cream(costs, coins));
17+
println!("{:?}", max_ice_cream(vec![1,3,2,4,1], 7));
18+
}

0 commit comments

Comments
 (0)