class SlideWindow:
def slide(self, arr, n, w):
qmax = [] #双端队列,队头记录最大值的下标
res = []
for i in range(n):
while qmax != []:
j = qmax[-1]
if arr[j] > arr[i]:
break
else:
qmax.pop()
qmax.append(i)
if i >= w-1:
if i-qmax[0]>=w:
qmax.pop(0)
res.append(arr[qmax[0]])
return res
网友评论