Number of Visible People in a Queue LeetCode solution in Java

1
class Solution {
2
public int[] canSeePersonsCount(int[] heights) {
3
Stack<Integer> stack = new Stack<>();
4
int result[] = new int[heights.length];
5
for (int i = heights.length - 1; i >= 0; i--) {
6
int visibility = 0;
7
while (!stack.isEmpty() && heights[i] > stack.peek()) {
8
stack.pop();
9
visibility++;
10
}
11
if (!stack.isEmpty()) visibility++;
12
stack.push(heights[i]);
13
result[i] = visibility;
14
}
15
return result;
16
}
17
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0