aboutsummaryrefslogtreecommitdiff
path: root/max_ice_cream_bars.rs
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /max_ice_cream_bars.rs
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'max_ice_cream_bars.rs')
-rw-r--r--max_ice_cream_bars.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/max_ice_cream_bars.rs b/max_ice_cream_bars.rs
new file mode 100644
index 0000000..6691a3b
--- /dev/null
+++ b/max_ice_cream_bars.rs
@@ -0,0 +1,27 @@
+struct Solution;
+
+impl Solution {
+ pub fn max_ice_cream(costs: Vec<i32>, coins: i32) -> i32 {
+ let (mut bars, mut coins, mut price, mut count) =
+ ([0_u32; 100_001], coins, 1, 0);
+ costs.into_iter().for_each(|c| bars[c as usize] += 1);
+ while coins >= price && price <= 100_000 {
+ while bars[price as usize] > 0 && coins >= price {
+ bars[price as usize] -= 1;
+ coins -= price;
+ count += 1;
+ }
+ price += 1;
+ }
+ count
+ }
+}
+
+fn main() {
+ let costs1 = vec![1,3,2,4,1];
+ let costs2 = vec![10,6,8,7,7,8];
+ let costs3 = vec![1,6,3,1,2,5];
+ println!("{}", Solution::max_ice_cream(costs1, 7)); //expect: 4
+ println!("{}", Solution::max_ice_cream(costs2, 5)); //expect: 0
+ println!("{}", Solution::max_ice_cream(costs3, 20)); //expect: 6
+}