Next Greater Element to the Right
Problem statement
Given an integer array nums, return an array answer of the same length. For each index i, answer[i] is the first value strictly greater than nums[i] at an index to its right.
If no strictly greater value exists to the right, set answer[i] to -1. The array is not circular.
Function
nextGreaterElements(nums: int[]) → int[]Examples
Example 1
nums = [2,1,2,4,3]return = [4,2,4,-1,-1]The first greater values to the right are 4, 2, and 4. The final two positions have none.
Example 2
nums = [1,3,2,4]return = [3,4,4,-1]Each answer uses the nearest later value that is strictly larger.
Example 3
nums = [5]return = [-1]The only element has no value to its right.
Constraints
1 <= nums.length <= 100000.-10^9 <= nums[i] <= 10^9.