This is the similiar problem with: https://www.cnblogs.com/feiflytech/p/16168587.html and https://www.cnblogs.com/feiflytech/p/16169025.html
class Solution { public int[] canSeePersonsCount(int[] heights) { int[] res = new int[heights.length]; Stack<Integer> stk = new Stack<>(); for(int i=0;i<heights.length;i++){ while(!stk.isEmpty()&& heights[i]>=heights[stk.peek()]){ res[stk.pop()]++; } if(!stk.isEmpty()) res[stk.peek()]++; stk.push(i); } return res; } }