503. Next Greater Element II
Practice Next Greater Element II for free on NudgeCode: Medium, Stack pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Treat nums as a circle: after the last element you wrap around to the first. For every index i, walk forward from i (wrapping at most once, and not counting nums[i] itself) and report the first value strictly greater than nums[i], or -1 if no such value exists. Values may repeat.
Example
nums = [1,2,1] -> [2,-1,2]
nums = [1,2,3,4,3] -> [2,3,4,-1,4]
Constraints
- 1 <= nums.length <= 10^4
- -10^9 <= nums[i] <= 10^9