239. Sliding Window Maximum
202407191130
tags: #sliding-window
var maxSlidingWindow = function(nums, k) {
if (!nums || k <= 0) {
return [];
}
const n = nums.length;
const deque = [];
const res = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length > 0 && deque[0] < i - k + 1) {
deque.shift();
}
while (deque.length > 0 && nums[deque[deque.length - 1]] < nums[i]) {
deque.pop();
}
deque.push(i);
if (i >= k - 1) {
res.push(nums[deque[0]]);
}
}
return res;
};