Intermediate
Open
Pro
Next Largest Number to the Right
Given an array of integers nums, for each element find the value
of the next element to its right that is strictly greater
than it. If no such element exists, use -1 for that position.
Return an array result of the same length as nums, where
result[i] is the next greater element for nums[i].
Example 1
Input: nums = [2, 1, 2, 4, 3]
Output: [4, 2, 4, -1, -1]
Explanation:
nums[0] = 2 -> next greater to the right is 4
nums[1] = 1 -> next greater to the right is 2
nums[2] = 2 -> next greater to the right is 4
nums[3] = 4 -> no greater element to the right -> -1
nums[4] = 3 -> no greater element to the right -> -1
Example 2
Input: nums = [5, 4, 3, 2, 1]
Output: [-1, -1, -1, -1, -1]
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Share this question