From d56d847328eb2d980342fff1f33b12f0ee5b84ac Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sat, 6 Jan 2024 14:00:01 +0800 Subject: [PATCH] Add problem 2033: Minimum Operations to Make a Uni-Value Grid --- src/lib.rs | 1 + .../median.rs | 57 +++++++++++++++++++ .../mod.rs | 23 ++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/problem_2033_minimum_operations_to_make_a_uni_value_grid/median.rs create mode 100644 src/problem_2033_minimum_operations_to_make_a_uni_value_grid/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 2b72430a..929368f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1403,6 +1403,7 @@ pub mod problem_2022_convert_1d_array_into_2d_array; pub mod problem_2027_minimum_moves_to_convert_string; pub mod problem_2028_find_missing_observations; pub mod problem_2032_two_out_of_three; +pub mod problem_2033_minimum_operations_to_make_a_uni_value_grid; pub mod problem_2037_minimum_number_of_moves_to_seat_everyone; pub mod problem_2038_remove_colored_pieces_if_both_neighbors_are_the_same_color; pub mod problem_2042_check_if_numbers_are_ascending_in_a_sentence; diff --git a/src/problem_2033_minimum_operations_to_make_a_uni_value_grid/median.rs b/src/problem_2033_minimum_operations_to_make_a_uni_value_grid/median.rs new file mode 100644 index 00000000..92516034 --- /dev/null +++ b/src/problem_2033_minimum_operations_to_make_a_uni_value_grid/median.rs @@ -0,0 +1,57 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::num::NonZeroU32; + +impl Solution { + pub fn min_operations(grid: Vec>, x: i32) -> i32 { + let x = NonZeroU32::new(x as _).unwrap(); + let mut values = Vec::with_capacity(grid.first().map_or(0, Vec::len) * grid.len()); + let remainder = grid[0][0] as u32 % x; + + for row in grid { + for value in row { + let value = value as u32; + + if value % x == remainder { + let value = value / x; + + values.push(value); + } else { + return -1; + } + } + } + + let n = values.len(); + let (left, &mut middle, right) = values.select_nth_unstable(n / 2); + let mut result = 0; + + for &mut value in left { + result += middle - value; + } + + for &mut value in right { + result += value - middle; + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn min_operations(grid: Vec>, x: i32) -> i32 { + Self::min_operations(grid, x) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2033_minimum_operations_to_make_a_uni_value_grid/mod.rs b/src/problem_2033_minimum_operations_to_make_a_uni_value_grid/mod.rs new file mode 100644 index 00000000..7ff8c216 --- /dev/null +++ b/src/problem_2033_minimum_operations_to_make_a_uni_value_grid/mod.rs @@ -0,0 +1,23 @@ +pub mod median; + +pub trait Solution { + fn min_operations(grid: Vec>, x: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + use crate::test_utilities::Matrix; + + pub fn run() { + let test_cases = [ + ((&[[2, 4], [6, 8]] as &dyn Matrix<_>, 2), 4), + ((&[[1, 5], [2, 3]], 1), 5), + ((&[[1, 2], [3, 4]], 2), -1), + ]; + + for ((grid, x), expected) in test_cases { + assert_eq!(S::min_operations(grid.to_vec(), x), expected); + } + } +}