1944. Number of Visible People in a Queue

Practice Number of Visible People in a Queue for free on NudgeCode: Hard, Stack pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

n people stand in a queue from left to right; heights[i] is the height of person i and all heights are distinct. Person i can see person j (i < j) if everyone strictly between them is shorter than both of them: min(heights[i], heights[j]) > max(heights[i+1], ..., heights[j-1]).

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

Example

heights = [10,6,8,5,11,9]  ->  [3,1,2,1,1,0]

(Person 0 sees persons 1, 2 and 4.)

Constraints