> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Buildings With an Ocean View Python Solution

> Tested Python solution for LeetCode 1762 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1762, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Stack](/catalog/topics/stack), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/buildings-with-an-ocean-view/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1762   # by problem number
lcpy gen -s buildings_with_an_ocean_view   # by problem name
```

## Problem

\<p>There are \<code>n\</code> buildings in a line. You are given an integer array \<code>heights\</code> of size \<code>n\</code> that represents the heights of the buildings in the line.\</p>

\<p>The ocean is to the right of the buildings. A building has an ocean view if the building can see the ocean without obstructions. Formally, a building has an ocean view if all the buildings to its right have a \<strong>smaller\</strong> height.\</p>

\<p>Return a list of indices \<strong>(0-indexed)\</strong> of buildings that have an ocean view, sorted in increasing order.\</p>

### Examples

```
Input: heights = [4,2,3,1]
Output: [0,2,3]
```

**Explanation:** Building 1 (0-indexed) does not have an ocean view because building 2 is taller.

```
Input: heights = [4,3,2,1]
Output: [0,1,2,3]
```

**Explanation:** All the buildings have an ocean view.

```
Input: heights = [1,3,2,4]
Output: [3]
```

**Explanation:** Only building 3 has an ocean view.

### Constraints

* 1 \<= heights.length \<= 10^5
* 1 \<= heights\[i] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/buildings_with_an_ocean_view/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/buildings_with_an_ocean_view/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) excluding the output list
    def find_buildings(self, heights: list[int]) -> list[int]:
        result: list[int] = []
        max_right = 0

        for i in range(len(heights) - 1, -1, -1):
            if heights[i] > max_right:
                result.append(i)
                max_right = heights[i]

        return result[::-1]
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) excluding the output list |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.