Next Greater Element
Next Greater Element
Given an integer array nums, return an array res where res[i] is the first element to the right of i that is strictly greater than nums[i]. If no such element exists, res[i] = -1.
Function signature
func NextGreaterElements(nums []int) []int
Example
nums = [2,1,2,4,3]
output = [4,2,4,-1,-1]
Constraints
- 0 <= len(nums) <= 200_000
- -1_000_000_000 <= nums[i] <= 1_000_000_000
Notes
- A monotonic decreasing stack solves this in O(n).
Run tests to see results
No issues detected