Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Number of Visible People in a Queue

XPChallenge Points: 30
levelLevel: Hard

You are given an array heights representing n people standing in a queue from left to right. Each value heights[i] is the height of person i, and all heights are unique.

A person at index i can see another person at index j (j > i) if everyone standing between them is shorter than both persons i and j.
In other words, person j is visible to person i if:

min(heights[i], heights[j]) > max(heights[i+1 ... j-1])

Your task is to return an array where each value answer[i] represents how many people person i can see looking to the right.

Example 1:

Input: heights = [12, 4, 7, 3, 15, 10]

Output: [2,1,1,1,1,0]

Explanation:

Person 0 sees persons 2 and 4 Person 1 sees 2 Person 2 sees 3 Person 3 sees 4 Person 4 sees 5 Person 5 sees no one

Example 2:

Input: heights = [9, 2, 6, 1, 5]

Output: [2,1,1,1,0]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result