Skip to content

Commit fc2b15d

Browse files
committed
Add problem 2380: Time Needed to Rearrange a Binary String
1 parent 96eedf3 commit fc2b15d

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,7 @@ 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;
17711771
pub mod problem_2379_minimum_recolors_to_get_k_consecutive_black_blocks;
1772+
pub mod problem_2380_time_needed_to_rearrange_a_binary_string;
17721773

17731774
#[cfg(test)]
17741775
mod test_utilities;
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 seconds_to_remove_occurrences(s: String) -> i32 {
7+
let mut zeroes = 0;
8+
let mut cost = 0_u16;
9+
10+
s.bytes().skip_while(|&c| c == b'1').for_each(|c| {
11+
if c == b'0' {
12+
zeroes += 1;
13+
} else {
14+
cost = zeroes.max(cost + 1);
15+
}
16+
});
17+
18+
i32::from(cost)
19+
}
20+
}
21+
22+
// ------------------------------------------------------ snip ------------------------------------------------------ //
23+
24+
impl super::Solution for Solution {
25+
fn seconds_to_remove_occurrences(s: String) -> i32 {
26+
Self::seconds_to_remove_occurrences(s)
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn seconds_to_remove_occurrences(s: String) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [("0110101", 4), ("11100", 0)];
13+
14+
for (s, expected) in test_cases {
15+
assert_eq!(S::seconds_to_remove_occurrences(s.to_string()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)