class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(k==0){
return new int[0];
}
int n = nums.length;
Deque<Integer> deque = new LinkedList<Integer>();
for (int i = 0; i < k; ++i) {
while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {
deque.pollLast();
}
deque.offerLast(i);
}
int[] ans = new int[n - k + 1];
ans[0] = nums[deque.peekFirst()];
for (int i = k; i < n; ++i) {
while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {
deque.pollLast();
}
deque.offerLast(i);
while (deque.peekFirst() <= i - k) {
deque.pollFirst();
}
ans[i - k + 1] = nums[deque.peekFirst()];
}
return ans;
}
}
相关文章
- 04-032021-10-05(剑指 Offer 59 - I. 滑动窗口的最大值)
- 04-03剑指offer59-II-队列的最大值-双向队列Deque
- 04-03LeetCode-239-剑指offer-滑动窗口的最大值-队列与栈-python
- 04-03【题解】剑指 Offer II 008. 和大于等于 target 的最短子数组(双指针)(不定长度滑动窗口)
- 04-03剑指 Offer 59 - II. 队列的最大值
- 04-03剑指 Offer 59 - II. 队列的最大值
- 04-03剑指 Offer 59 - II. 队列的最大值
- 04-03剑指 Offer 59 - II. 队列的最大值
- 04-03leetcode剑指 Offer 59 - I. 滑动窗口的最大值
- 04-03单调队列的应用:剑指 Offer 59 - II. 队列的最大值 双端队列(图文并茂)一步一步详细指导