diff --git a/src/lib.rs b/src/lib.rs index e3a321b8..fa054406 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1650,6 +1650,7 @@ pub mod problem_2207_maximize_number_of_subsequences_in_a_string; pub mod problem_2208_minimum_operations_to_halve_array_sum; pub mod problem_2209_minimum_white_tiles_after_covering_with_carpets; pub mod problem_2210_count_hills_and_valleys_in_an_array; +pub mod problem_2211_count_collisions_on_a_road; pub mod problem_2215_find_the_difference_of_two_arrays; pub mod problem_2216_minimum_deletions_to_make_array_beautiful; pub mod problem_2218_maximum_value_of_k_coins_from_piles; diff --git a/src/problem_2211_count_collisions_on_a_road/iterative.rs b/src/problem_2211_count_collisions_on_a_road/iterative.rs new file mode 100644 index 00000000..fb4baac4 --- /dev/null +++ b/src/problem_2211_count_collisions_on_a_road/iterative.rs @@ -0,0 +1,79 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +enum State { + AllLeft, + Stop, + Right(u32), +} + +impl Solution { + pub fn count_collisions(directions: String) -> i32 { + let mut result = 0; + let mut state = State::AllLeft; + let mut iter = directions.bytes(); + + loop { + match state { + State::AllLeft => { + if let Some(c) = iter.next() { + match c { + b'L' => {} + b'R' => state = State::Right(1), + _ => state = State::Stop, + } + } else { + break; + } + } + State::Stop => { + if let Some(c) = iter.next() { + match c { + b'L' => result += 1, + b'R' => state = State::Right(1), + _ => {} + } + } else { + break; + } + } + State::Right(count) => { + if let Some(c) = iter.next() { + match c { + b'L' => { + result += count + 1; + state = State::Stop; + } + b'R' => state = State::Right(count + 1), + _ => { + result += count; + state = State::Stop; + } + } + } else { + break; + } + } + } + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn count_collisions(directions: String) -> i32 { + Self::count_collisions(directions) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2211_count_collisions_on_a_road/iterative_2.rs b/src/problem_2211_count_collisions_on_a_road/iterative_2.rs new file mode 100644 index 00000000..6ab41456 --- /dev/null +++ b/src/problem_2211_count_collisions_on_a_road/iterative_2.rs @@ -0,0 +1,32 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn count_collisions(directions: String) -> i32 { + let directions = directions.into_bytes(); + let left = directions.iter().take_while(|&&c| c == b'L').count(); + let right = directions.iter().rev().take_while(|&&c| c == b'R').count(); + + directions[left..directions.len() - right] + .iter() + .filter(|&&c| c != b'S') + .count() as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn count_collisions(directions: String) -> i32 { + Self::count_collisions(directions) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2211_count_collisions_on_a_road/mod.rs b/src/problem_2211_count_collisions_on_a_road/mod.rs new file mode 100644 index 00000000..25524fd5 --- /dev/null +++ b/src/problem_2211_count_collisions_on_a_road/mod.rs @@ -0,0 +1,19 @@ +pub mod iterative; +pub mod iterative_2; + +pub trait Solution { + fn count_collisions(directions: String) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [("RLRSLL", 5), ("LLRR", 0), ("LLSRSSRSSLLSLLLRSLSRL", 11), ("L", 0)]; + + for (directions, expected) in test_cases { + assert_eq!(S::count_collisions(directions.to_string()), expected); + } + } +}