155.最小栈
155.最小栈
239.滑动窗口最大值
239.滑动窗口最大值
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums == null || nums.length == 0 || k < 1) return new int[0];
if(k == 1) return nums;
int[] maxes = new int[nums.length - k + 1];
//peek:取值
//poll:删除
//offer:添加
Deque<Integer> deque = new LinkedList<>();
for(int ri = 0; ri <nums.length; ri++){
//只要nums[队尾] <= nums[i] 就删除队尾
while(!deque.isEmpty() && nums[ri] >= nums[deque.peekLast()]){
deque.removeLast();
}
//将i加到队尾
deque.addLast(ri);
//检查窗口索引是否合法
int li = ri - k + 1;
if(li < 0) continue;
//检查队头合法性
if(deque.peekFirst() < li){
//队头不合法(失效,不在滑动窗口索引范围内)
deque.removeFirst();
}
//设置窗口最大值
maxes[li] = nums[deque.peekFirst()];
}
return maxes;
}
}