-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path239. 滑动窗口最大值.py
33 lines (30 loc) · 881 Bytes
/
239. 滑动窗口最大值.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from collections import deque
from typing import List
class MonotonicQueue(object):
def __init__(self,maxlen):
self.data=deque(maxlen=maxlen)
def push(self,n):
while self.data and self.data[-1]<n:
self.data.pop()
self.data.append(n)
def pop(self,n):
if self.data and self.data[0]==n:
self.data.popleft()
def max(self):
return self.data[0]
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
m=MonotonicQueue(k)
res=[]
for i in range(len(nums)):
if i<k-1:
m.push(nums[i])
else:
m.push(nums[i])
res.append(m.max())
m.pop(nums[i-k+1])
return res
s=Solution()
nums = [1,3,-1,-3,5,3,6,7]
k=3
print(s.maxSlidingWindow(nums,k))