Interview Query

Visible Buildings

Start Timer

0:00:00

Upvote
0
Downvote
Save question
Mark as completed
View comments (1)
Next question

You are given a list of positive integers representing the heights of consecutive buildings. You can remove any number of buildings, but you cannot change the order of the remaining buildings. Write a function max_visible_buildings that takes the list of building heights as input and returns the maximum number of buildings that can be visible from the left side of the street.

Note: the first building is always visible. The rest of them are only visible if they’re higher than the building on their left.

Example:

Input:

heights = [1, 3, 5, 4, 6, 3, 7]

Output:

def max_visible_buildings(heights) -> 
output = 5

Explanation:

In this example, we can remove the building with height 5, leaving us with the buildings [1, 3, 4, 6, 3, 7].

In this new sequence, buildings [1,3,4,6,7] are visible.

.
.
.
.
.


Comments

Loading comments