-
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 2134: Minimum Swaps to Group All 1's Together II
- Loading branch information
Showing
3 changed files
with
71 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_2134_minimum_swaps_to_group_all_1s_together_ii/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 sliding_window; | ||
|
||
pub trait Solution { | ||
fn min_swaps(nums: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[0, 1, 0, 1, 1, 0, 0] as &[_], 1), | ||
(&[0, 1, 1, 1, 0, 0, 1, 1, 0], 2), | ||
(&[1, 1, 0, 0, 1], 0), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::min_swaps(nums.to_vec()), expected); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/problem_2134_minimum_swaps_to_group_all_1s_together_ii/sliding_window.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,48 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn min_swaps(nums: Vec<i32>) -> i32 { | ||
let n = nums.len() as u32; | ||
let ones = nums.iter().sum::<i32>() as u32; | ||
|
||
let (left, right) = nums.split_at(ones as usize); | ||
let mut window_ones = left.iter().sum::<i32>() as u32; | ||
let mut max_windows_ones = window_ones; | ||
|
||
for (&old, &new) in nums.iter().zip(right) { | ||
window_ones -= old as u32; | ||
window_ones += new as u32; | ||
max_windows_ones = max_windows_ones.max(window_ones); | ||
} | ||
|
||
let (left, right) = nums.split_at((n - ones) as usize); | ||
let mut window_ones = left.iter().sum::<i32>() as u32; | ||
let mut min_windows_ones = window_ones; | ||
|
||
for (&old, &new) in nums.iter().zip(right) { | ||
window_ones -= old as u32; | ||
window_ones += new as u32; | ||
min_windows_ones = min_windows_ones.min(window_ones); | ||
} | ||
|
||
(ones - max_windows_ones).min(min_windows_ones) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_swaps(nums: Vec<i32>) -> i32 { | ||
Self::min_swaps(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |