> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/meta-2025/1762.-buildings-with-an-ocean-view.md).

# 1762. Buildings With an Ocean View

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 can see the ocean without obstructions. Formally, a building has an ocean view if all the buildings to its right have a **smaller** height.

Return a list of indices **(0-indexed)** of buildings that have an ocean view, sorted in increasing order.

&#x20;

**Example 1:**

<pre><code><strong>Input: heights = [4,2,3,1]
</strong><strong>Output: [0,2,3]
</strong><strong>Explanation: Building 1 (0-indexed) does not have an ocean view because building 2 is taller.
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: heights = [4,3,2,1]
</strong><strong>Output: [0,1,2,3]
</strong><strong>Explanation: All the buildings have an ocean view.
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: heights = [1,3,2,4]
</strong><strong>Output: [3]
</strong><strong>Explanation: Only building 3 has an ocean view.
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= heights.length <= 10`<sup>`5`</sup>
* `1 <= heights[i] <= 10`<sup>`9`</sup>

分析

单调栈

```python3
class Solution:
    def findBuildings(self, heights: List[int]) -> List[int]:
        stack = []
        for i,h in enumerate(heights):
            while stack and heights[stack[-1]] <= h:
                stack.pop()
            
            stack.append(i)
        return stack
        
            
            
            
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/meta-2025/1762.-buildings-with-an-ocean-view.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
