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