-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2171: Removing Minimum Number of Magic Beans
- Loading branch information
Showing
3 changed files
with
62 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
18 changes: 18 additions & 0 deletions
18
src/problem_2171_removing_minimum_number_of_magic_beans/mod.rs
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,18 @@ | ||
pub mod sorting; | ||
|
||
pub trait Solution { | ||
fn minimum_removal(beans: Vec<i32>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(&[4, 1, 6, 5] as &[_], 4), (&[2, 10, 3, 2], 7)]; | ||
|
||
for (beans, expected) in test_cases { | ||
assert_eq!(S::minimum_removal(beans.to_vec()), expected); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/problem_2171_removing_minimum_number_of_magic_beans/sorting.rs
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,43 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn minimum_removal(beans: Vec<i32>) -> i64 { | ||
let mut beans = beans.into_iter().map(|x| x as u32).collect::<Vec<_>>(); | ||
|
||
beans.sort_unstable(); | ||
|
||
let mut left_sum = 0; | ||
let mut right_length = beans.len() as u64; | ||
let mut right_sum = beans.iter().fold(0_u64, |sum, &x| sum + u64::from(x)); | ||
|
||
beans.into_iter().fold(u64::MAX, |mut result, value| { | ||
let value = u64::from(value); | ||
|
||
result = result.min(left_sum + (right_sum - value * right_length)); | ||
|
||
left_sum += value; | ||
right_length -= 1; | ||
right_sum -= value; | ||
|
||
result | ||
}) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_removal(beans: Vec<i32>) -> i64 { | ||
Self::minimum_removal(beans) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |