239. Sliding Window Maximum [Hard]
作者:互联网
/**
* monotonic queue
*/
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int[] res = new int[nums.length - k + 1];
LinkedList<Integer> queue = new LinkedList<>(); // to sotre and update the index of possible max value in the current window
for (int i = 0; i < nums.length; i++) {
while (!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]) {
queue.pollLast();
}
while (!queue.isEmpty() && queue.peek() <= i - k) {
queue.poll();
}
queue.offer(i);
if (i >= k - 1) {
res[i - k + 1] = nums[queue.peek()];
}
}
return res;
}
}
标签:LinkedList,nums,int,res,Hard,queue,Window,239,new 来源: https://blog.csdn.net/Squirrelmi/article/details/120646262