20241016 finished.

This commit is contained in:
jackfiled 2024-10-16 13:31:30 +08:00
parent b282d4c001
commit b41dc85d53
2 changed files with 39 additions and 1 deletions

View File

@ -264,4 +264,5 @@ mod p3164_find_the_number_of_good_pairs_ii;
mod p3158_find_the_xor_of_numbers_which_appear_twice;
mod p1884_egg_drop_with_2_eggs_and_n_floors;
mod p887_super_egg_drop;
mod p3200_maximum_height_of_a_triangle;
mod p3200_maximum_height_of_a_triangle;
mod p3194_minimum_average_of_smallest_and_largest_elements;

View File

@ -0,0 +1,37 @@
/**
* [3194] Minimum Average of Smallest and Largest Elements
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn minimum_average(nums: Vec<i32>) -> f64 {
let mut nums: Vec<f64> = nums.into_iter().map(|x| x as f64).collect();
nums.sort_by(|a, b| a.partial_cmp(b).unwrap());
let n = nums.len();
let mut result = f64::MAX;
for i in 0..n / 2 {
result = result.min((nums[i] + nums[n - i - 1]) / 2f64);
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3194() {
assert_eq!(5.5, Solution::minimum_average(vec![7, 8, 3, 4, 15, 13, 4, 1]));
assert_eq!(5.5, Solution::minimum_average(vec![1, 9, 8, 3, 10, 5]));
assert_eq!(5.0, Solution::minimum_average(vec![1, 2, 3, 7, 8, 9]));
}
}