-
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 1944: Number of Visible People in a Queue
- Loading branch information
Showing
3 changed files
with
67 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
21 changes: 21 additions & 0 deletions
21
src/problem_1944_number_of_visible_people_in_a_queue/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,21 @@ | ||
pub mod stack; | ||
|
||
pub trait Solution { | ||
fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[10, 6, 8, 5, 11, 9] as &[_], &[3, 1, 2, 1, 1, 0] as &[_]), | ||
(&[5, 1, 2, 3, 10], &[4, 1, 1, 1, 0]), | ||
]; | ||
|
||
for (heights, expected) in test_cases { | ||
assert_eq!(S::can_see_persons_count(heights.to_vec()), expected); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/problem_1944_number_of_visible_people_in_a_queue/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,45 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32> { | ||
let mut stack = Vec::new(); | ||
let mut heights = heights; | ||
|
||
for target in heights.iter_mut().rev() { | ||
let mut count = 0; | ||
|
||
while let Some(&top) = stack.last() { | ||
count += 1; | ||
|
||
if top < *target { | ||
stack.pop(); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
stack.push(*target); | ||
*target = count; | ||
} | ||
|
||
heights | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32> { | ||
Self::can_see_persons_count(heights) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |