Monotonic stack · Medium
A monitoring chart annotates each recorded measurement with the first later reading that exceeds it. Equal readings do not end the wait, and the annotation needs the later value rather than its index or the largest value in the suffix.
Return the first strictly higher value to the right of each measurement and null when no such value exists.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[] }
Fields
- nums lists signed measurements in time order.
Output
Return value
- Return an array of the same length, aligned with the original indexes. Use null when there is no strictly greater reading.
- A later value of -1 is an ordinary answer, not an absence marker.
Constraints
- 0 <= nums.length <= 100000; each value is an integer between -1000000000 and 1000000000.
Examples
Example 1
{
"nums": [
2,
2,
3,
1
]
}Returns
[
3,
3,
null,
null
]Example 2
{
"nums": [
-2,
-1
]
}Returns
[
-1,
null
]Required complexity
O(n) time and O(n) auxiliary space