ALG-M014MediumMonotonic Stack
Next Higher Reading
For each index, find the value of the first strictly greater element to its right. Return -1 when no greater value exists.
Contract
input numbers: integer[]
return integer[]
Constraints
- • 0 <= length <= 100000
Examples
Example 1
numbers = [2, 1, 2, 4, 3]
→ [4, 2, 4, -1, -1]
Example 2
numbers = [5, 4, 3]
→ [-1, -1, -1]
5 judge tests (2 shown above). Target complexity: O(n).