FunctionRacerProblemsRace

Visible to the Right

MediumStacks and queuesPython, JavaScript, TypeScript, Java, C++

People stand in a row and heights[i] is the height of the person at position i. Everyone looks to the right. Person i can see person j (with j > i) when every person standing strictly between them is shorter than both heights[i] and heights[j]. Neighbours always see each other, and a person of equal or greater height in between blocks the view.

Return a list where item i is the number of people person i can see. The last person sees nobody. Return an empty list for an empty row.

Example 1

heights [10,6,8,5,11,9]
returns [3,1,2,1,1,0]

Person 0 (height 10) sees the people of height 6, 8 and 11; the 11 hides everyone after it. Person 2 (height 8) sees 5 and 11.

Example 2

heights [5,1,2,3,10]
returns [4,1,1,1,0]

Person 0 (height 5) sees everyone, because 1, 2 and 3 rise without blocking and the 10 is at the end.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems