Ctrl K

Towers With a Sea View

Medium
A row of towers stands along the waterfront; their heights are listed in towers from left to right. The sea lies to the right of the last tower. A tower can see the sea if every tower to its right is strictly shorter. Return the 0-based indices of all towers with a sea view, in increasing order.
Example 1
towers=[5, 2, 4, 4, 1] → [0, 3, 4]

Tower 2 is blocked by tower 3 of the same height, tower 1 by the taller ones to its right. Towers 0, 3 and 4 see the sea.

Example 2
towers=[2, 3, 7] → [2]

The rightmost tower is also the tallest and blocks everyone else.

Example 3
towers=[9, 6, 3] → [0, 1, 2]
Constraints
  • 1 ≤ len(towers) ≤ 10⁵
  • 1 ≤ towers[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPreorder of a Search Tree MediumLow, Peak, Middle MediumNext Larger Around the Circle MediumFix the Middle of the Line MediumPeak-Split Tree Medium
loading Python…
Input
towers=[5, 2, 4, 4, 1]
Expected
[0, 3, 4]

Run checks the first 3 tests in your browser. Submit checks all 131 tests on the server, hidden ones included.