Mid/Senior

Buildings With an Ocean View

There are n buildings in a line, and the ocean is to the right of all the buildings. You are given an integer array heights of length n, where heights[i] represents the height of the ith building.

A building has an ocean view if every building to its right has a strictly smaller height. Return a list of indices of buildings that have an ocean view, sorted in increasing order.

Example 1
#
#   #
# # #
# # # #
4 2 3 1
Inputheights = [4,2,3,1]
Output[0,2,3]
Building 0 can see over buildings 1, 2, and 3; building 2 can see over building 3; building 3 has no buildings to its right.
Example 2
#
# #
# # #
# # # #
4 3 2 1
Inputheights = [4,3,2,1]
Output[0,1,2,3]
Each building is taller than all buildings to its right, so every building has an ocean view.

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate