There are n people standing in a queue, and they numbered from 0 to n - 1 in left to right order. You are given an array heights of distinct integers where heights[i] represents the height of the ith person.

A person can see another person to their right in the queue if everybody in between is shorter than both of them. More formally, the ith person can see the jth person if i &lt; j and min(heights[i], heights[j]) &gt; max(heights[i+1], heights[i+2], ..., heights[j-1]).

Return an array answer of length n where answer[i] is the number of people the ith person can see to their right in the queue.

&nbsp;
Example 1:



Input: heights = [10,6,8,5,11,9]
Output: [3,1,2,1,1,0]
Explanation:
Person 0 can see person 1, 2, and 4.
Person 1 can see person 2.
Person 2 can see person 3 and 4.
Person 3 can see person 4.
Person 4 can see person 5.
Person 5 can see no one since nobody is to the right of them.


Example 2:

Input: heights = [5,1,2,3,10]
Output: [4,1,1,1,0]


&nbsp;
Constraints:


	n == heights.length
	1 &lt;= n &lt;= 105
	1 &lt;= heights[i] &lt;= 105
	All the values of heights are unique.


class Solution:
    def canSeePersonsCount(self, heights: List[int]) -> List[int]:
        ans=[]
        stack=[]
        n=len(heights)
        for i in range(n-1,-1,-1):
            if len(stack)==0:
                ans.append(0)
                stack.append(heights[i])
            else:
                if heights[i]<stack[-1]:
                    ans.append(1)
                    stack.append(heights[i])
                else:
                    ctr=0
                    while(len(stack)>0 and stack[-1]<heights[i]):
                        ctr+=1
                        stack.pop()
                    if len(stack)==0:
                        ans.append(ctr)
                    else:
                        ans.append(ctr+1)
                    stack.append(heights[i])
        return ans[::-1]
class Solution {
    public int[] canSeePersonsCount(int[] heights) {
        Stack<Integer> stack = new Stack<>();
        int result[] = new int[heights.length];
        for(int i = heights.length - 1; i >= 0; i--) {
            int visibility = 0;
            while(!stack.isEmpty() && heights[i] > stack.peek()) {
                stack.pop();
                visibility++;
            }
            if(!stack.isEmpty()) visibility++;
            stack.push(heights[i]);
            result[i] = visibility;
        }
        return result;
    }
}
class Solution {
public:
    vector<int> canSeePersonsCount(vector<int>& h) {
        vector<int>ans ;
        stack<int>s ;
        int a = 0;
        for(int i = h.size()-1 ; i >= 0 ; i--){
            if(s.empty()){
                ans.push_back(a);
                s.push(h[i]);a++;
            }
            else{
                if(s.top() > h[i]){
                    ans.push_back(1);
                    s.push(h[i]);a++;
                }
                else{
                int b = 0;
                while(!s.empty() && s.top() < h[i]){
                    s.pop() ; a--;b++;
                }
                if(!s.empty())b++;
                ans.push_back(b);
                s.push(h[i]); a++;
                } 
            }
        }
        reverse(ans.begin() , ans.end());
        return ans;
    }
};
var visibleToRight = function(heights){
    const result = new Array(heights.length).fill(0)
    const stack = []

    let i = heights.length-1
    
    // loop from right to left
    while(i >= 0){ 
        let popCount = 0
        
        // Keep Popping untill top ele is smaller
        while(stack.length > 0 && stack[stack.length-1][0] < heights[i]){
			stack.pop()
            popCount+=1
        }
        
        /////////////////////
        /// After Popping ///
        ////////////////////
        
        // Case1: if ALL elements got popped
        if(stack.length === 0)
            result[i] = popCount // mark
        
        // Case2: if NO elements were popped
        else if(popCount === 0)
            result[i] = 1 // mark
            
        // Case3: if SOME elements were popped
        else
            result[i] = popCount+1 // mark
        
        // store
        stack.push([heights[i],popCount])
        
        i-=1
    }
    
    return result
}
var canSeePersonsCount = function(heights) {
    return visibleToRight(heights)
};
Number of Visible People in a Queue