-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |