631. Number of Visible People in Queue

MediumStackMonotonic StackStack

People with distinct heights stand in a line. Person i can see person j (j > i) if everyone strictly between them is shorter than both. For each person, return how many people to their right they can see. The input is JSON {heights}.

Input: JSON {heights}.

Output: Array — the count of visible people to the right of each.

Examples

Example 1
Input: {"heights":[10,6,8,5,11,9]}
Output: [3,1,2,1,1,0]
Explanation: Each person's rightward visibility.
Example 2
Input: {"heights":[5,1,2,3,10]}
Output: [4,1,1,1,0]
Explanation: Person 0 sees 1,2,3 and the taller 10.
Example 3
Input: {"heights":[7]}
Output: [0]
Explanation: No one to the right.

Constraints

Asked by

GoogleOracleMicrosoftMetaAmazonBloomberg
Solve this problem in the editor →