#1944
Number of Visible People in a Queue
specialist · 955 · lc hard +32 · premium · verified · 72.5% accepted · 2,176 likes · top 82%
Description
There are n people standing in a queue, labeled 0 to n - 1 from left to right, with heights given by array heights (all distinct). Person i can see person j (to their right) if every person between them has height strictly less than both heights[i] and heights[j].
Return array answer where answer[i] is the number of people person i can see to the right.
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]
Code
1
2
3