From 9b83dfbdb24b661718a9261eeac85142e39978bc Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 18 Jan 2024 11:44:50 +0800 Subject: [PATCH] 20240118 Finished --- src/problem/mod.rs | 3 +- ..._removing_minimum_number_of_magic_beans.rs | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2171_removing_minimum_number_of_magic_beans.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index d2ccbf9..40ca739 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -19,4 +19,5 @@ mod p2182_construct_string_with_repeat_limit; mod p83_remove_duplicates_from_sorted_list; mod p82_remove_duplicates_from_sorted_list_ii; mod p2719_count_of_integers; -mod p2744_find_maximum_number_of_string_pairs; \ No newline at end of file +mod p2744_find_maximum_number_of_string_pairs; +mod p2171_removing_minimum_number_of_magic_beans; \ No newline at end of file diff --git a/src/problem/p2171_removing_minimum_number_of_magic_beans.rs b/src/problem/p2171_removing_minimum_number_of_magic_beans.rs new file mode 100644 index 0000000..c4f7b13 --- /dev/null +++ b/src/problem/p2171_removing_minimum_number_of_magic_beans.rs @@ -0,0 +1,41 @@ +/** + * [2171] Removing Minimum Number of Magic Beans + */ +pub struct Solution {} + + +// submission codes start here + +use std::cmp::min; +impl Solution { + pub fn minimum_removal(beans: Vec) -> i64 { + let mut beans = beans; + beans.sort_unstable(); + + let mut sum = 0i64; + for i in &beans { + sum += *i as i64; + } + + let mut result = i64::MAX; + for (index, value) in (&beans).iter().enumerate() { + result = min(result, + sum - (*value as i64) * (beans.len() - index) as i64); + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2171() { + assert_eq!(Solution::minimum_removal(vec![4,1,6,5]), 4); + assert_eq!(Solution::minimum_removal(vec![2,10,3,2]), 7); + } +}