20240118 Finished

This commit is contained in:
jackfiled 2024-01-18 11:44:50 +08:00
parent 21be9f2c0f
commit 9b83dfbdb2
2 changed files with 43 additions and 1 deletions

View File

@ -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;
mod p2744_find_maximum_number_of_string_pairs;
mod p2171_removing_minimum_number_of_magic_beans;

View File

@ -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<i32>) -> 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);
}
}