1.双端队列法
/* * @lc app=leetcode.cn id=239 lang=javascript * * [239] 滑动窗口最大值 */// @lc code=start/** * @param {number[]} nums * @param {number} k * @return {number[]} */var maxSlidingWindow = function (nums, k) { if (nums.length === 0 || !k) return [] let windows = [] let res = [] for (let i = 0; i < nums.length; i++) { if (windows[0] !== undefined && windows[0] <= i - k) windows.shift() while (nums[windows[windows.length - 1]] <= nums[i]) windows.pop() windows.push(i) if (i >= k - 1) res.push(nums[windows[0]]) } return res}// @lc code=end