Next Bigger Number
The integers in nums stand in a circle: after the last number comes the first again. For every number, find the first number bigger than it that you meet when walking around the circle from it, in the direction of the list. If no number in the circle is bigger, the answer for it is -1.
Return the answers as a list, in the order of nums.
Examples
nums = [2, 7, 3, 5] → [7, -1, 5, 7]
nums = [4, 3, 2, 1] → [-1, 4, 4, 4]
nums = [5, 5, 5] → [-1, -1, -1]
nums = [1, 2, 1] → [2, -1, 2]In the first example, the walk from 5 goes past the end, back to 2, then 7, the first bigger number. Nothing is bigger than 7, so its answer is -1. An equal number is not bigger. Numbers can be negative, so a -1 in the answer is either "nothing bigger" or a real -1 met on the walk: [-1, -3, -2] gives [-1, -2, -1].
Constraints
- Between 0 and 10,000 numbers, each between -1,000,000,000 and 1,000,000,000.
- Aim for O(n): every number should be waited on, and answered, once.
Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.