AlgoMaster Logo

Buildings With an Ocean View

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We have a row of buildings, and the ocean sits to the right. A building can "see" the ocean only if every building between it and the ocean is shorter than it. In other words, no building to the right is as tall or taller.

The rightmost building always has an ocean view since there's nothing blocking it. For every other building, we need to check whether anything to its right is at least as tall. The requirement is "strictly taller", so a building of the same height as its tallest right neighbor does not have an ocean view.

We don't need to compare each building with every building to its right. Whether a building has a view depends on a single value: the maximum height among the buildings to its right. If a building is taller than that maximum, it has an ocean view.

Key Constraints:

  • 1 <= heights.length <= 10^5 → With n up to 100,000, an O(n^2) approach can reach 10 billion comparisons. We want O(n).
  • 1 <= heights[i] <= 10^9 → Heights are at least 1, so a running maximum initialized to 0 is always beaten by the first building compared against it. The algorithms only compare heights, never add them, so 32-bit integers are safe.

Approach 1: Brute Force

Intuition

For each building, check every building to its right. If none of them is as tall or taller, that building has an ocean view. This translates the problem statement directly into code: for building i, scan from i+1 to n-1, and stop early as soon as a building with height greater than or equal to heights[i] blocks the view.

Algorithm

  1. Create an empty result list.
  2. For each building at index i from left to right:
    • Set a flag hasView = true.
    • For each building at index j from i+1 to the end:
      • If heights[j] >= heights[i], set hasView = false and break.
    • If hasView is still true, add i to the result.
  3. Return the result list.

Visualization and Code

Loading animation...

The waste is in the rescanning: the check for building 0 walks nearly the whole array, and the check for building 1 walks almost the same elements again. A right-to-left pass that carries a running maximum answers the same question for every building with a single comparison.

Approach 2: Right-to-Left Scan with Running Maximum

Intuition

A building has an ocean view if and only if it is taller than every building to its right, which is the same as being taller than the maximum height to its right. Scanning from right to left makes that maximum cheap to maintain: it is the maximum of the heights already visited.

The rightmost building always sees the ocean. Moving left, each building only needs to beat the running maximum, not compare against every individual building between it and the ocean. If it does, it has a view and becomes the new maximum. One detail remains: we collect indices from right to left, so the result must be reversed before returning to satisfy the increasing-order requirement.

Algorithm

  1. Initialize maxHeight = 0 and an empty result list.
  2. Iterate from the last building to the first (right to left).
  3. For each building at index i:
    • If heights[i] > maxHeight, this building has an ocean view. Add i to the result.
    • Update maxHeight = max(maxHeight, heights[i]).
  4. Reverse the result list (since we collected indices right to left, but the output needs increasing order).
  5. Return the result.

Visualization and Code

Loading animation...

This is already optimal in time and space. But there's an alternative O(n) approach using a monotonic stack that generalizes better to related problems like "next greater element."

Approach 3: Monotonic Stack (Left-to-Right)

Intuition

Instead of scanning right to left, we can scan left to right using a stack. Maintain a stack of building indices that, so far, have an unblocked view. Each new building pops every stacked building whose height is less than or equal to its own, because the new building blocks them. The pop condition is <= rather than < since the problem requires strictly taller: a building of equal height blocks the view too. Whatever remains on the stack after the last building is processed has an ocean view.

Algorithm

  1. Create an empty stack.
  2. Iterate through buildings from left to right.
  3. For each building at index i:
    • While the stack is not empty and heights[stack.peek()] <= heights[i], pop from the stack (these buildings are now blocked).
    • Push i onto the stack.
  4. The stack now contains the indices of all buildings with an ocean view, in increasing order.
  5. Convert the stack to an array and return it.

Visualization and Code

Loading animation...