from collections import deque
class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
n=len(nums)
res=[None]*(n-k+1)
q=deque()
for i in range(n):
start=i-k+1
while q and i-q[0]>=k:
q.popleft()
while q and nums[q[-1]]<=nums[i]:
q.pop()
q.append(i)
if start>=0:
res[start]=nums[q[0]]
return res
Leetcode 239. 滑动窗口最大值
最新推荐文章于 2025-08-05 16:42:55 发布