From 8fa4a0d241998f5a24f2362dca008e6940849f48 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Mon, 13 Nov 2023 23:14:32 +0800 Subject: [PATCH] Add problem 1674: Minimum Moves to Make Array Complementary --- src/lib.rs | 1 + .../mod.rs | 22 +++++++++ .../sweep_line.rs | 47 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/problem_1674_minimum_moves_to_make_array_complementary/mod.rs create mode 100644 src/problem_1674_minimum_moves_to_make_array_complementary/sweep_line.rs diff --git a/src/lib.rs b/src/lib.rs index bd171c4d..734e82cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1372,6 +1372,7 @@ pub mod problem_1670_design_front_middle_back_queue; pub mod problem_1671_minimum_number_of_removals_to_make_mountain_array; pub mod problem_1672_richest_customer_wealth; pub mod problem_1673_find_the_most_competitive_subsequence; +pub mod problem_1674_minimum_moves_to_make_array_complementary; pub mod problem_1675_minimize_deviation_in_array; pub mod problem_1678_goal_parser_interpretation; pub mod problem_1679_max_number_of_k_sum_pairs; diff --git a/src/problem_1674_minimum_moves_to_make_array_complementary/mod.rs b/src/problem_1674_minimum_moves_to_make_array_complementary/mod.rs new file mode 100644 index 00000000..20ecfdee --- /dev/null +++ b/src/problem_1674_minimum_moves_to_make_array_complementary/mod.rs @@ -0,0 +1,22 @@ +pub mod sweep_line; + +pub trait Solution { + fn min_moves(nums: Vec, limit: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ((&[1, 2, 4, 3] as &[_], 4), 1), + ((&[1, 2, 2, 1], 2), 2), + ((&[1, 2, 1, 2], 2), 0), + ]; + + for ((nums, k), expected) in test_cases { + assert_eq!(S::min_moves(nums.to_vec(), k), expected); + } + } +} diff --git a/src/problem_1674_minimum_moves_to_make_array_complementary/sweep_line.rs b/src/problem_1674_minimum_moves_to_make_array_complementary/sweep_line.rs new file mode 100644 index 00000000..587ad5aa --- /dev/null +++ b/src/problem_1674_minimum_moves_to_make_array_complementary/sweep_line.rs @@ -0,0 +1,47 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn min_moves(nums: Vec, limit: i32) -> i32 { + let limit = limit as u32 as usize; + let mut diffs = vec![0; limit * 2 + 1].into_boxed_slice(); + let (left, right) = nums.split_at(nums.len() / 2); + + for (&x, &y) in left.iter().zip(right.iter().rev()) { + let (x, y) = (x as u32 as usize, y as u32 as usize); + let (min, max) = if y < x { (y, x) } else { (x, y) }; + + diffs[min] -= 1; + diffs[min + max - 1] -= 1; + diffs[min + max] += 1; + diffs[max + limit] += 1; + } + + let mut moves = nums.len() as i32; + let mut result = moves; + + for &diff in &diffs[1..] { + moves += diff; + result = result.min(moves); + } + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn min_moves(nums: Vec, limit: i32) -> i32 { + Self::min_moves(nums, limit) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}