All problems
0474MediumArrayStackMonotonic Stack

Silos With a Clear Line to the River

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1762Buildings With an Ocean View

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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