程序员代码面试指南 python实现(生成窗口最大值的数组)
程序员代码面试指南 python实现(第一章 栈和队列)
生成窗口最大值的数组
题目描述
def getMaxWindow(arr,w):
if arr == [] or w <1 or len(arr) < w:
return
qmax = Queue()
res = []
index = 0
for i in range(len(arr)):
while qmax.isEmpty() != True and arr[qmax.peekLast()] <= arr[i]:
qmax.pollLast()
qmax.add(i)
if qmax.peekFirst() == i-w:
qmax.pollFirst()
if i >=w-1:
res[index] = arr[qmax.peekFirst()]
index += 1
return res