美文网首页
【数组】滑动窗口的最大值

【数组】滑动窗口的最大值

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-08-26 18:42 被阅读0次
    # -*- coding:utf-8 -*-
    class Solution:
        def maxInWindows(self, num, size):
            # write code here
            length = len(num)
            if length == 0 or length < size:
                return []
            qu, res = [], []
            for i in range(len(num)):
                while qu and num[qu[-1]] < num[i]:
                    qu.pop()
                if qu and qu[0] <= i - size:
                    qu.pop(0)
                qu.append(i)
                if size and i+1>=size:
                    res.append(num[qu[0]])
            return res
    

    相关文章

      网友评论

          本文标题:【数组】滑动窗口的最大值

          本文链接:https://www.haomeiwen.com/subject/wmidectx.html