Skip to content

Commit

Permalink
finish maximum ice cream bars
Browse files Browse the repository at this point in the history
  • Loading branch information
Checky-Hu committed Apr 29, 2021
1 parent c629cec commit 7b4817d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 37-50/maximum-ice-cream-bars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "maximum-ice-cream-bars"
version = "0.1.0"
authors = ["Checky Hu <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
46 changes: 46 additions & 0 deletions 37-50/maximum-ice-cream-bars/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::env;
use std::str::FromStr;

struct Solution {}

impl Solution {
pub fn max_ice_cream(costs: Vec<i32>, coins: i32) -> i32 {
let mut costs_mut: Vec<i32> = costs;
costs_mut.sort();
let mut ret: i32 = 0;
let mut sum: i32 = 0;
for cost in costs_mut.iter() {
sum += *cost;
if sum > coins {
break;
} else {
ret += 1;
}
}
ret
}
}

fn main() {
let mut ret: usize = 0;
let mut coins: i32 = 0;
let mut costs: Vec<i32> = Vec::new();
for (index, arg) in env::args().enumerate() {
match index {
0 => (),
1 => coins = i32::from_str(&arg).expect("Error parse."),
_ => {
ret += 1;
let cost: i32 = i32::from_str(&arg).expect("Error parse.");
costs.push(cost);
}
}
}

if 0 == ret {
println!("Require at least 2 parameters.");
return;
}

println!("Max ice cream: {}", Solution::max_ice_cream(costs, coins));
}

0 comments on commit 7b4817d

Please sign in to comment.