Next Greater Element II
Input:
[1,2,1]
Output:
[2,-1,2]
Explanation:
The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.Analysis
Solution
Monotonous Stack + Circular Array Search (21 ms, faster than 94.64%)
A little optimization with i < len and checking if stack.isEmpty() then break
i < len and checking if stack.isEmpty() then breakLast updated