There are n buildings in a line. You are given an integer array heights of size n that represents the heights of the buildings in the line.
The ocean is to the right of the buildings. A building has an ocean view if the building is strictly taller than all the buildings to its right.
Return a list of the indices of buildings that have an ocean view, sorted in ascending order.
Example 1:
Input: heights = [4,2,3,1] Output: [0,2,3] Explanation: Building at index 0 has a height of 4 and is taller than all the other buildings, so it has an ocean view. Building at index 2 has a height of 3 and is taller than all the buildings to its right, so it has an ocean view. Building at index 3 has a height of 1 and is taller than all the buildings to its right, so it has an ocean view.
Example 2:
Input: heights = [4,3,2,1] Output: [0,1,2,3] Explanation: All the buildings have an ocean view.
Example 3:
Input: heights = [1,3,2,4] Output: [3] Explanation: Only the building at index 3 has an ocean view.
Constraints:
1 <= heights.length <= 1051 <= heights[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding buildings with an ocean view is to check each building individually. We'll assume a building has an ocean view unless we find evidence otherwise by checking all buildings to its right.
Here's how the algorithm would work step-by-step:
def buildings_with_ocean_view_brute_force(building_heights):
buildings_with_view = []
number_of_buildings = len(building_heights)
for current_building_index in range(number_of_buildings):
has_ocean_view = True
# Check all buildings to the right
for comparison_building_index in range(current_building_index + 1, number_of_buildings):
# Check if a taller building is blocking view
if building_heights[comparison_building_index] >= building_heights[current_building_index]:
has_ocean_view = False
break
# If no taller building was found, add to result
if has_ocean_view:
buildings_with_view.append(current_building_index)
return buildings_with_viewThe goal is to find buildings with an unobstructed ocean view, meaning no taller building blocks its view to the right. The trick is to check buildings from right to left, keeping track of the tallest building we've seen so far. This way, we only need to look at each building once.
Here's how the algorithm would work step-by-step:
def find_buildings_with_ocean_view(building_heights):
ocean_view_buildings = []
tallest_building_so_far = 0
# Iterate backwards as rightmost always has view
for index in range(len(building_heights) - 1, -1, -1):
current_building_height = building_heights[index]
# Check if current building has ocean view
if current_building_height > tallest_building_so_far:
ocean_view_buildings.append(index)
# Update the tallest building so far.
tallest_building_so_far = current_building_height
ocean_view_buildings.reverse()
return ocean_view_buildings| Case | How to Handle |
|---|---|
| Empty input array | Return an empty list as there are no buildings to consider. |
| Input array with only one building | Return a list containing only the index 0, as a single building always has an ocean view. |
| Buildings with monotonically increasing heights | Every building has an ocean view, so return a list of indices from 0 to n-1. |
| Buildings with monotonically decreasing heights | Only the last building has an ocean view, so return a list containing only the last index. |
| All buildings have the same height | Only the last building has an ocean view, so return a list containing only the last index. |
| Very large input array (performance considerations) | Iterate from right to left, keeping track of the maximum height seen so far, to achieve O(n) time complexity. |
| Input array contains zero height buildings | Zero height buildings are valid and should be processed like any other height. |
| Input array contains large integer heights (potential overflow) | Use integer comparison and avoid arithmetic operations that could lead to overflow, which can be achieved by comparing heights directly. |