# Buildings With an Ocean View

### Problem

{% embed url="<https://leetcode.com/problems/buildings-with-an-ocean-view/description/>" %}

<figure><img src="https://1388126071-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVQ7Ie-PZpN_8yApX0-%2Fuploads%2FrSnweJ7KNFhpL6wTePPv%2Fimage.png?alt=media&#x26;token=3b9f3cf8-fd1a-483a-91e3-87fd57fc8899" alt=""><figcaption></figcaption></figure>

### Solution

```java
class Solution {
    public int[] findBuildings(int[] heights) {
        // Time Complexity: O(n), iterate through heights
        // Space Complexity: O(n), storage for stack
        Stack<Integer> stack = new Stack<>();

        // Here we are intersted in the strictly decreasing heights of building and
        // no other metric, so we can return the builidngs height indexes directly
        for(int i=0;i<heights.length;i++) {
            // As the building can have an ocean view iff there are no obsructions
            // So to make sure of that, when we are traversing from left->right,
            // the building heights have to be strictly decreasing
            while(!stack.isEmpty() && heights[stack.peek()] <= heights[i]) {
                stack.pop();
            }
            stack.push(i);
        }
        return stack.stream().mapToInt(i->i).toArray();
        
    }
}
```
