From ddfffac29d7a49547c8c568236e4d2f69bd50fbb Mon Sep 17 00:00:00 2001 From: EFanZh Date: Thu, 4 Jul 2024 21:45:23 +0800 Subject: [PATCH] Add problem 2256: Minimum Average Difference --- src/lib.rs | 1 + .../iterative.rs | 55 +++++++++++++++++++ .../mod.rs | 18 ++++++ 3 files changed, 74 insertions(+) create mode 100644 src/problem_2256_minimum_average_difference/iterative.rs create mode 100644 src/problem_2256_minimum_average_difference/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 5c5b17fc..10dca8ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1581,6 +1581,7 @@ pub mod problem_2243_calculate_digit_sum_of_a_string; pub mod problem_2244_minimum_rounds_to_complete_all_tasks; pub mod problem_2248_intersection_of_multiple_arrays; pub mod problem_2255_count_prefixes_of_a_given_string; +pub mod problem_2256_minimum_average_difference; pub mod problem_2259_remove_digit_from_number_to_maximize_result; pub mod problem_2260_minimum_consecutive_cards_to_pick_up; pub mod problem_2262_total_appeal_of_a_string; diff --git a/src/problem_2256_minimum_average_difference/iterative.rs b/src/problem_2256_minimum_average_difference/iterative.rs new file mode 100644 index 00000000..ef904090 --- /dev/null +++ b/src/problem_2256_minimum_average_difference/iterative.rs @@ -0,0 +1,55 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn minimum_average_difference(nums: Vec) -> i32 { + let mut left_sum = 0; + let mut left_count = 0; + let mut right_sum = nums.iter().fold(0_u64, |sum, &x| sum + u64::from(x as u32)); + let mut right_count = nums.len() as u64; + let mut min_diff = u64::MAX; + let mut min_diff_index = 0; + + for num in nums { + let num = u64::from(num as u32); + + left_count += 1; + right_count -= 1; + left_sum += num; + right_sum -= num; + + let left_average = left_sum / left_count; + let right_average = if right_count == 0 { 0 } else { right_sum / right_count }; + + let diff = left_average.abs_diff(right_average); + + if diff < min_diff { + min_diff = diff; + min_diff_index = left_count; + + if diff == 0 { + break; + } + } + } + + (min_diff_index - 1) as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn minimum_average_difference(nums: Vec) -> i32 { + Self::minimum_average_difference(nums) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2256_minimum_average_difference/mod.rs b/src/problem_2256_minimum_average_difference/mod.rs new file mode 100644 index 00000000..6a4778d2 --- /dev/null +++ b/src/problem_2256_minimum_average_difference/mod.rs @@ -0,0 +1,18 @@ +pub mod iterative; + +pub trait Solution { + fn minimum_average_difference(nums: Vec) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [(&[2, 5, 3, 9, 5, 3] as &[_], 3), (&[0], 0)]; + + for (nums, expected) in test_cases { + assert_eq!(S::minimum_average_difference(nums.to_vec()), expected); + } + } +}