双向队列好
import collections
class Solution(object):
def maxSlidingWindow(self, nums, k):
res=[]
d = collections.deque()
for i in range(len(nums)):
if len(d)==0:
d.append((nums[i],i))
if i>=k-1:
res.append(d[0][0])
continue
if d[0][1]<=i-k:
d.popleft()
while len(d)>0:
tail=d[-1][0]
if nums[i]<tail:
break
d.pop()
d.append((nums[i],i))
if i>=k-1:
res.append(d[0][0])
return res
网友评论