class Solution {
public int[] nextGreaterElements(int[] nums) {
Stack<Integer> stack=new Stack<>();
int n=nums.length;
int[] res=new int[n];
for(int i=2*n-1;i>=0;i--){
while(!stack.empty()&&nums[i%n]>=stack.peek()){
stack.pop();
}
res[i%n]=stack.empty()? -1:stack.peek();
stack.push(nums[i%n]);
}
return res;
}
}
单调栈解决循环数组的下一个更大元素
猜你喜欢
转载自blog.csdn.net/changbaishannefu/article/details/115267347
今日推荐
周排行