Next Greater Element II
Given a circular integer array nums (the element after the last wraps around to the first), return an array where each position holds the next greater number when searching forward circularly. If no greater number exists, use -1.
Open official problem prompt ↗For each element, find the first strictly larger value that appears when scanning forward and wrapping around the end of the array.
Imagine people standing in a circle by height, each looking clockwise for the first person taller than them. A short person keeps looking until someone taller appears; that taller person 'answers' everyone shorter who was still searching.
- Input
- nums = [1, 2, 1]
- Output
- [2, -1, 2]
- Why
- For nums[0]=1 the next greater is 2; for nums[1]=2 nothing larger exists; for nums[2]=1 the search wraps around and finds 2.
1 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9