Buildings With an Ocean View
Medium · Stack & Monotonic Stack · about 10 minutes
Asked at Meta, Amazon, Google.
Buildings stand in a line with heights heights[i], and the sea lies past the last one on the right. A building can see the sea when every building between it and the sea is strictly lower than it. Return, in increasing order, the indices of the buildings that can see the sea.