Skip to the code
cf.
+50 XP

#270 Who Can See Whom in a Team Photo

hard

For a team page, people stand in a row from left to right with distinct heights heights. Person i can see person j to their right (i < j) if everyone standing between them is shorter than both of them.

Write visibleCounts(heights) that returns an array where position i is how many people to the right person i can see.

Example 1

Input: heights = [10,6,8,5,11,9]
Output: [3,1,2,1,1,0]

Person 0 sees 6, 8, and 11; 5 is hidden behind 8, and 9 behind 11.

Example 2

Input: heights = [5,1,2,3,10]
Output: [4,1,1,1,0]

Person 0 sees everyone: each person between is shorter than both.

Constraints

  • 1 <= heights.length <= 10^5
  • 1 <= heights[i] <= 10^5
  • All heights are distinct.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).