All problems
0178MediumArrayStackMonotonic Stack

Loop Trail Next Rise

Tracked in this browser only
Write code

Trains the technique from

LeetCode 503Next Greater Element II

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.

A survey crew walked a closed loop trail and wrote down the signed elevation change at each marker, in walking order, as the array deltas. The trail closes on itself, so once you step past the final marker you are back at marker 0.

Stand at a marker and keep walking forward around the loop until you reach a marker whose elevation change is strictly larger than the one you started from. Report that larger change. If you make a full circuit without finding one, report -1 instead.

Return an array answer of the same length, where answer[i] is what you report from marker i. Elevation changes may themselves be negative, and -1 is a perfectly legal change, so a reported -1 carries no extra meaning: it may be a change of -1 that you walked to, or it may be the signal that the loop holds nothing larger.

Examples

Example 1

Input
deltas = [2, 7, 1]
Output
[7, -1, 2]

Marker 0 finds 7 one step ahead. Marker 1 holds the largest change on the loop, so it reports -1. Marker 2 has to wrap past the end to reach 2.

Example 2

Input
deltas = [-4, -9, -2]
Output
[-2, -2, -1]

Both of the first two markers walk forward to -2. Marker 2 already holds the largest change, so it reports -1.

Example 3

Input
deltas = [6, 6, 6]
Output
[-1, -1, -1]

An equal change is not a rise, so no marker ever finds one.

Constraints

  • 1 <= deltas.length <= 10^4
  • -10^9 <= deltas[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 next_rise(deltas: list[int]) -> list[int]:
Java
public int[] nextRise(int[] deltas)
September 7
Apply