Next Greater Element to the Right
Problem statement
For every position in an integer array, return the first strictly greater value at a later position.
For this exercise, assume a linear left-to-right sequence with no wraparound. If there is no later strictly greater value, return -1 for that position. Equal values do not qualify. Return values rather than indices, in input position order.
Function
nextGreaterToRight(nums: int[]) → int[]Examples
Example 1
nums = [2,1,2,4,3]return = [4,2,4,-1,-1]The first 2 skips equal 2 and reaches 4; the final two positions have no later larger value.
Example 2
nums = [4,1,3]return = [-1,3,-1]The last value has no later element, even though the first element is larger.
Example 3
nums = [2,2,3]return = [3,3,-1]Equal values do not resolve one another; both first positions use 3.
Constraints
- For this exercise, assume
0 <= nums.length <= 2000. -10^6 <= nums[i] <= 10^6.