lettcode239. 滑动窗口最大值
困难 
解:
普通解法超出时间限制,降低复杂度
演示动画:
https://pic.leetcode-cn.com/1625035801-ICSAmO-572d9ba6221dffa4ebc29b738685192b.gif
let nums = [1, 3, 1, 2, 0, 5], k = 3var maxSlidingWindow = function (nums, k) {// 队列数组(存放的是元素下标,为了取值方便)const q = [];// 结果数组const ans = [];for (let i = 0; i < nums.length; i++) {// 若队列不为空,且当前元素大于等于队尾所存下标的元素,则弹出队尾console.log(nums[i], q);while (q.length && nums[i] >= nums[q[q.length - 1]]) {console.log('--');q.pop();}// 入队当前元素下标q.push(i);// 判断当前最大值(即队首元素)是否在窗口中,若不在便将其出队while (q[0] <= i - k) {console.log('++')q.shift();}// 当达到窗口大小时便开始向结果中添加数据if (i >= k - 1) ans.push(nums[q[0]]);}return ans;};console.log(maxSlidingWindow(nums, k));
