Ctrl K

Longest Stone Route at Each Stop

Hard
Stepping stones are laid in a row across a stream; heights[i] is the height of stone i. A route is a set of stones taken from left to right (not necessarily adjacent) where every next stone is at least as high as the one before it. For every index i, find the length of the longest route that ends exactly on stone i (it includes that stone and uses only stones with indices 0 to i). Return the array of these lengths.
Example 1
heights=[4, 2, 4, 3, 5, 1] → [1, 1, 2, 2, 3, 1]

For the stone of height 5 (index 4) the route 2 → 3 → 5 or 2 → 4 → 5 works: three stones. The last stone, of height 1, is lower than everything before it, so its route is just itself.

Example 2
heights=[6, 6, 2, 6] → [1, 2, 1, 3]

Equal heights may follow each other: the route 6 → 6 → 6 of length 3 reaches the last stone.

Constraints
  • 1 ≤ len(heights) ≤ 10⁵
  • 1 ≤ heights[i] ≤ 10⁷
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardCost of Filling a Sorted Shelf HardTriples Both Rankings Agree On Hard
loading Python…
Input
heights=[4, 2, 4, 3, 5, 1]
Expected
[1, 1, 2, 2, 3, 1]

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