Number of Visible People in a Queue LeetCode solution in Rust

1
impl Solution {
2
pub fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32> {
3
let n = heights.len();
4
let mut stk: Vec<i32> = Vec::new();
5
let mut ans: Vec<i32> = vec![0; n];
6
for i in (0..n).rev() {
7
let mut cnt = 0;
8
while (!stk.is_empty() && heights[i] > stk[stk.len() - 1]) {
9
stk.pop();
10
cnt += 1;
11
}
12
ans[i] = if stk.is_empty() { cnt } else { cnt + 1 };
13
stk.push(heights[i]);
14
}
15
return ans;
16
}
17
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0