-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1964: Find the Longest Valid Obstacle Course at Each Posi…
…tion
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pub mod stack; | ||
|
||
pub trait Solution { | ||
fn longest_obstacle_course_at_each_position(obstacles: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
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, | ||
); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/problem_1964_find_the_longest_valid_obstacle_course_at_each_position/stack.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn longest_obstacle_course_at_each_position(obstacles: Vec<i32>) -> Vec<i32> { | ||
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<i32>) -> Vec<i32> { | ||
Self::longest_obstacle_course_at_each_position(obstacles) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |