From d7f99fa3807cae8be2bcef89429f29c9de4e6c1f Mon Sep 17 00:00:00 2001 From: EFanZh Date: Mon, 19 Aug 2024 21:59:35 +0800 Subject: [PATCH] Add problem 1964: Find the Longest Valid Obstacle Course at Each Position --- src/lib.rs | 1 + .../mod.rs | 26 ++++++++++++ .../stack.rs | 41 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/mod.rs create mode 100644 src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/stack.rs diff --git a/src/lib.rs b/src/lib.rs index 32dadb43..596b308a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1458,6 +1458,7 @@ pub mod problem_1959_minimum_total_space_wasted_with_k_resizing_operations; pub mod problem_1961_check_if_string_is_a_prefix_of_array; pub mod problem_1962_remove_stones_to_minimize_the_total; pub mod problem_1963_minimum_number_of_swaps_to_make_the_string_balanced; +pub mod problem_1964_find_the_longest_valid_obstacle_course_at_each_position; pub mod problem_1967_number_of_strings_that_appear_as_substrings_in_word; pub mod problem_1968_array_with_elements_not_equal_to_average_of_neighbors; pub mod problem_1969_minimum_non_zero_product_of_the_array_elements; diff --git a/src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/mod.rs b/src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/mod.rs new file mode 100644 index 00000000..347b13fb --- /dev/null +++ b/src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/mod.rs @@ -0,0 +1,26 @@ +pub mod stack; + +pub trait Solution { + fn longest_obstacle_course_at_each_position(obstacles: Vec) -> Vec; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (&[1, 2, 3, 2] as &[_], &[1, 2, 3, 3] as &[_]), + (&[2, 2, 1], &[1, 2, 1]), + (&[3, 1, 5, 6, 4, 2], &[1, 1, 2, 3, 2, 2]), + (&[5, 1, 5, 5, 1, 3, 4, 5, 1, 4], &[1, 1, 2, 3, 2, 3, 4, 5, 3, 5]), + ]; + + for (obstacles, expected) in test_cases { + assert_eq!( + S::longest_obstacle_course_at_each_position(obstacles.to_vec()), + expected, + ); + } + } +} diff --git a/src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/stack.rs b/src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/stack.rs new file mode 100644 index 00000000..ebbc2a97 --- /dev/null +++ b/src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/stack.rs @@ -0,0 +1,41 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn longest_obstacle_course_at_each_position(obstacles: Vec) -> Vec { + let mut obstacles = obstacles; + let mut lengths = Vec::with_capacity(obstacles.len()); + + for height in &mut obstacles { + let height_u32 = *height as u32; + let i = lengths.partition_point(|&x| x <= height_u32); + + if let Some(value) = lengths.get_mut(i) { + *value = height_u32; + } else { + lengths.push(height_u32); + } + + *height = i as i32 + 1; + } + + obstacles + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn longest_obstacle_course_at_each_position(obstacles: Vec) -> Vec { + Self::longest_obstacle_course_at_each_position(obstacles) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}