-
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 1864: Minimum Number of Swaps to Make the Binary String A…
…lternating
- Loading branch information
Showing
3 changed files
with
63 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
44 changes: 44 additions & 0 deletions
44
src/problem_1864_minimum_number_of_swaps_to_make_the_binary_string_alternating/greedy.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,44 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
fn fold_count(count: u16, c: u8) -> u16 { | ||
count + u16::from(c) - u16::from(b'0') | ||
} | ||
|
||
pub fn min_swaps(s: String) -> i32 { | ||
let n = s.len() as u16; | ||
let ones = s.bytes().fold(0, Self::fold_count); | ||
let zeros = n - ones; | ||
let diff_plus_1 = (ones + 1).wrapping_sub(zeros); | ||
|
||
if diff_plus_1 < 3 { | ||
let even_ones = s.bytes().step_by(2).fold(0, Self::fold_count); | ||
|
||
i32::from(match diff_plus_1 { | ||
0 => even_ones, | ||
1 => even_ones.min((n + 1) / 2 - even_ones), | ||
_ => (n + 1) / 2 - even_ones, | ||
}) | ||
} else { | ||
-1 | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_swaps(s: String) -> i32 { | ||
Self::min_swaps(s) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1864_minimum_number_of_swaps_to_make_the_binary_string_alternating/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,18 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn min_swaps(s: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [("111000", 1), ("010", 0), ("1110", -1), ("1", 0)]; | ||
|
||
for (s, expected) in test_cases { | ||
assert_eq!(S::min_swaps(s.to_string()), expected); | ||
} | ||
} | ||
} |