Skip to content

Commit 96eedf3

Browse files
committed
Add problem 2379: Minimum Recolors to Get K Consecutive Black Blocks
1 parent c11d6bf commit 96eedf3

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,7 @@ pub mod problem_2369_check_if_there_is_a_valid_partition_for_the_array;
17681768
pub mod problem_2370_longest_ideal_subsequence;
17691769
pub mod problem_2373_largest_local_values_in_a_matrix;
17701770
pub mod problem_2374_node_with_highest_edge_score;
1771+
pub mod problem_2379_minimum_recolors_to_get_k_consecutive_black_blocks;
17711772

17721773
#[cfg(test)]
17731774
mod test_utilities;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub mod sliding_window;
2+
pub mod sliding_window_2;
3+
4+
pub trait Solution {
5+
fn minimum_recolors(blocks: String, k: i32) -> i32;
6+
}
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::Solution;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [(("WBBWWBBWBW", 7), 3), (("WBWBBBW", 2), 0)];
14+
15+
for ((blocks, k), expected) in test_cases {
16+
assert_eq!(S::minimum_recolors(blocks.to_string(), k), expected);
17+
}
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn minimum_recolors(blocks: String, k: i32) -> i32 {
7+
let blocks = blocks.as_bytes();
8+
let (left, right) = blocks.split_at(k as u32 as _);
9+
let mut white_count = left.iter().fold(0, |count, &c| count + u8::from(c == b'W'));
10+
let mut result = white_count;
11+
12+
for (&old, &new) in blocks.iter().zip(right) {
13+
white_count -= u8::from(old == b'W');
14+
white_count += u8::from(new == b'W');
15+
result = result.min(white_count);
16+
}
17+
18+
i32::from(result)
19+
}
20+
}
21+
22+
// ------------------------------------------------------ snip ------------------------------------------------------ //
23+
24+
impl super::Solution for Solution {
25+
fn minimum_recolors(blocks: String, k: i32) -> i32 {
26+
Self::minimum_recolors(blocks, k)
27+
}
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
#[test]
33+
fn test_solution() {
34+
super::super::tests::run::<super::Solution>();
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn minimum_recolors(blocks: String, k: i32) -> i32 {
7+
let blocks = blocks.as_bytes();
8+
let (left, right) = blocks.split_at(k as u32 as usize - 1);
9+
let mut white_count = left.iter().fold(0, |count, &c| count + u8::from(c == b'W'));
10+
let mut result = u8::MAX;
11+
12+
for (&old, &c) in blocks.iter().zip(right) {
13+
white_count += u8::from(c == b'W');
14+
result = result.min(white_count);
15+
white_count -= u8::from(old == b'W');
16+
}
17+
18+
i32::from(result)
19+
}
20+
}
21+
22+
// ------------------------------------------------------ snip ------------------------------------------------------ //
23+
24+
impl super::Solution for Solution {
25+
fn minimum_recolors(blocks: String, k: i32) -> i32 {
26+
Self::minimum_recolors(blocks, k)
27+
}
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
#[test]
33+
fn test_solution() {
34+
super::super::tests::run::<super::Solution>();
35+
}
36+
}

0 commit comments

Comments
 (0)