首页 > 其他 > 详细

Leetcode 904. Fruit Into Baskets

时间:2019-03-14 22:08:11      阅读:182      评论:0      收藏:0      [点我收藏+]

sliding window(滑动窗口)算法

class Solution(object):
    def totalFruit(self, tree):
        """
        :type tree: List[int]
        :rtype: int
        """
        count=collections.Counter()
        left=ans=0
        for i,val in enumerate(tree):
            count[val]+=1
            while (len(count)>=3):
                count[tree[left]]-=1
                if count[tree[left]]==0:
                    del count[tree[left]]
                left+=1
            ans=max(ans,i-left+1)
        return ans     

 

Leetcode 904. Fruit Into Baskets

原文:https://www.cnblogs.com/zywscq/p/10533747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!