Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums. The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.
Example 1
Input: nums = [1,2,1]
Output: [2,-1,2]
Explanation: The first 1 sees 2 right after it. 2 is the largest value, so it gives -1. The last 1 wraps around to the front and finds 2.
Example 2
Input: nums = [1,2,3,4,3]
Output: [2,3,4,-1,4]
Explanation: Each of 1, 2 and 3 is followed by a larger number. 4 is the maximum, so it gives -1. The final 3 wraps around and reaches 4 first.
Example 3
Input: nums = [5,4,3,2,1]
Output: [-1,5,5,5,5]
Explanation: 5 is the maximum, so it gives -1. Every other number wraps around to the front and finds 5 first.
Simulate the circular array by iterating 2·n times with index i%n. A decreasing stack of indices resolves each element's next-greater; only push indices in the first pass.
Run checks all cases above. Submit evaluates all test cases.