题目链接:https://leetcode-cn.com/problems/sliding-window-maximum/
难度:困难
描述:
给你一个整数数组nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k个数字。滑动窗口每次只向右移动一位。
返回滑动窗口中的最大值
提示:
数组长度:[1, 100000]
窗口长度:[1, len(nums)]
题解
class Solution:def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:length = len(nums)q = collections.deque()for i in range(k):while q and nums[i] >= nums[q[-1]]:q.pop()q.append(i)ans = [nums[q[0]]]for i in range(k, length):while q and nums[i] >= nums[q[-1]]:q.pop()q.append(i)while q[0] <= i - k:q.popleft()ans.append(nums[q[0]])return ans
