20240118 Finished
This commit is contained in:
parent
21be9f2c0f
commit
9b83dfbdb2
|
@ -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;
|
41
src/problem/p2171_removing_minimum_number_of_magic_beans.rs
Normal file
41
src/problem/p2171_removing_minimum_number_of_magic_beans.rs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user