Show problems as
Classic problem #1762MediumArrayStackMonotonic Stack

Buildings With an Ocean View

Real-world version: Silos With a Clear Line to the River
Tracked in this browser only
Read Buildings With an Ocean View on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Grain silos stand in one straight line along a rail spur. heights[i] is the height of the silo at index i, and the indices run from the far end of the spur towards the river, so the river lies just past the last silo in the list.

A silo has a clear line to the river when every silo standing between it and the river is strictly shorter than it is. A silo of exactly the same height blocks the line, and the silo closest to the river has nothing in front of it at all.

Return the indices of all silos with a clear line to the river, in increasing order.

Examples

Example 1

Input
heights = [3, 8, 8, 2, 5, 5, 1]
Output
[2, 5, 6]

Indices 2, 5 and 6 each stand taller than everything left between them and the river. Index 4 is beaten by the equally tall silo at index 5, so it is left out.

Example 2

Input
heights = [6, 6, 6]
Output
[2]

All three silos match in height, so only the one nearest the river keeps its line.

Example 3

Input
heights = [11, 4, 9, 9, 2]
Output
[0, 3, 4]

Index 0 towers over the rest of the row, index 3 only has the short silo at index 4 in front of it, and index 4 is nearest the river.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def silos_with_river_view(heights: list[int]) -> list[int]:
Java
public int[] silosWithRiverView(int[] heights)