Line of Sight

Start Timer

0:00:00

Upvote
0
Downvote
Save question
Mark as completed
View comments

People are standing in a line. The array heights gives their heights in order. A person can see shorter people in either direction until someone at least as tall blocks the view. The blocking person is not counted. Count the person themselves as well.

Write max_visible(heights) to return the largest count for any person.

Example 1

heights = [5, 1, 2, 3, 1]
max_visible(heights)  # 5

The first person sees all four people to their right: each is shorter than 5. Including themselves gives 5. A shorter person does not block their view, even if that person is taller than someone farther away.

Example 2

heights = [4, 1, 4, 2]
max_visible(heights)  # 3

The person at index 2 sees the people at indices 1 and 3. The equal-height person at index 0 blocks further vision and is excluded. Including themselves gives 3.

For this practice version: heights are positive integers, the array may contain up to 100,000 people, and an empty array returns 0. Aim for an O(n) solution without changing the input.

.
.
.
.
.


Comments

Loading comments