Buildings With an Ocean View - Problem

Imagine you're a city planner designing a coastal neighborhood where residents want to enjoy beautiful ocean views! ๐Ÿ™๏ธ๐ŸŒŠ

You have n buildings arranged in a straight line along the coast. Each building has a specific height given in an array heights, where heights[i] represents the height of the i-th building. The ocean is located to the right of all buildings.

A building has an ocean view if and only if all buildings to its right are shorter than it. In other words, there are no taller buildings blocking its view to the ocean.

Your task: Return a list of indices (0-indexed) of all buildings that have an unobstructed ocean view, sorted in increasing order.

Example: If buildings have heights [4,2,3,1], then buildings at indices 0, 2, 3 have ocean views because:

  • Building 0 (height 4): All buildings to its right (2,3,1) are shorter โœ…
  • Building 1 (height 2): Building at index 2 (height 3) is taller โŒ
  • Building 2 (height 3): Building to its right (height 1) is shorter โœ…
  • Building 3 (height 1): No buildings to its right โœ…

Input & Output

example_1.py โ€” Basic case
$ Input: [4,2,3,1]
โ€บ Output: [0,2,3]
๐Ÿ’ก Note: Building 0 (height 4) can see ocean as all buildings to right (2,3,1) are shorter. Building 1 (height 2) cannot see ocean because building 2 (height 3) blocks it. Building 2 (height 3) can see ocean as building 3 (height 1) is shorter. Building 3 (height 1) can see ocean as no buildings are to its right.
example_2.py โ€” Increasing heights
$ Input: [4,3,2,1]
โ€บ Output: [0,1,2,3]
๐Ÿ’ก Note: All buildings have ocean view because heights are in decreasing order from left to right, so each building can see over all buildings to its right.
example_3.py โ€” Single building
$ Input: [1]
โ€บ Output: [0]
๐Ÿ’ก Note: With only one building, it automatically has an ocean view since there are no buildings to its right to block the view.

Visualization

Tap to expand
42310123View โœ“View โœ“View โœ“โœ—๐ŸŒŠ OCEANBuildings with Ocean ViewGreen lines show unobstructed ocean viewsResult: [0, 2, 3]
Understanding the Visualization
1
Setup the buildings
Arrange buildings in a line with ocean to the right
2
Start from rightmost
The rightmost building always has an ocean view
3
Scan left
Move left, tracking the tallest building seen so far
4
Check visibility
Each building taller than the current maximum can see the ocean
Key Takeaway
๐ŸŽฏ Key Insight: By scanning from right to left and tracking the maximum height, we can determine ocean visibility in a single pass, making this an O(n) solution instead of O(nยฒ).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array from right to left, each element processed once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables (not counting output array)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค heights.length โ‰ค 105
  • 1 โ‰ค heights[i] โ‰ค 109
  • The ocean is to the right of the buildings
Asked in
Meta 45 Amazon 32 Google 28 Microsoft 18
42.5K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen