File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
problem_2380_time_needed_to_rearrange_a_binary_string Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -1769,6 +1769,7 @@ pub mod problem_2370_longest_ideal_subsequence;
17691769pub mod problem_2373_largest_local_values_in_a_matrix;
17701770pub mod problem_2374_node_with_highest_edge_score;
17711771pub 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) ]
17741775mod test_utilities;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments