-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1674: Minimum Moves to Make Array Complementary
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/problem_1674_minimum_moves_to_make_array_complementary/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pub mod sweep_line; | ||
|
||
pub trait Solution { | ||
fn min_moves(nums: Vec<i32>, limit: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
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); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/problem_1674_minimum_moves_to_make_array_complementary/sweep_line.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn min_moves(nums: Vec<i32>, 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<i32>, limit: i32) -> i32 { | ||
Self::min_moves(nums, limit) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |