Next Greater Element
MediumP1Coding
For each element of nums, find the next greater element: the first value to its right that is strictly larger. Return an array of those values, using -1 where no greater element exists to the right.
Example 1
- Input
- nums = [4,5,2,25]
- Output
- [5,25,25,-1]
Example 2
- Input
- nums = [13,7,6,12]
- Output
- [-1,12,12,-1]
Example 3
- Input
- nums = [2,2,2]
- Output
- [-1,-1,-1]
- Why
- Equal values are not greater.
Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.