Leetcode 904. Fruit Into Baskets
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的更多相关文章
- [LeetCode] 904. Fruit Into Baskets 水果装入果篮
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- 【LeetCode】904. Fruit Into Baskets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/fruit-in ...
- LeetCode - Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- [Swift]LeetCode904. 水果成篮 | Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
随机推荐
- Happy Hours, Happy Days
No matter our age, being happy creates more happiness--making a better world for all of us. 无论青春与否,让 ...
- Shell Script Notes
shell Script学习笔记 <鸟哥的Linux私房菜 3rd Edition>: @1:若变量内容中包含空格字符,则可使用双引号"或单引号'将变量内容结合起来,但不同的是: ...
- Mapinfo修改道路方向
在mapinfp工具管理添加插件Reverse Line Direction,就可以修改道路方向 插件
- Git配置出现的问题
git是代码版本同步工具,适用于团队开发,进公司第一堂课就是配置Git.接下来就把其中遇到的问题记录一下,与大家共享一下. 首先,在Bitbucket上注册账户,之后给管理员说一下,让他邀请你加入开发 ...
- Service Fusing
服务熔断也称服务隔离,来自于Michael Nygard 的<Release It>中的CircuitBreaker应用模式,Martin Fowler在博文CircuitBreaker中 ...
- 每天一个Linux命令(40)vmstat命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控. 它能够对系统的整体情况进行统计,无法对某个进程 ...
- MACHINE_START-内核板级初始化实现机制(linux3.1.0)
转:https://blog.csdn.net/charliewangg12/article/details/41518549 在驱动开发时,我们都是以一块开发板为基础移植驱动程序.每一块开发板对应一 ...
- arguments解析
js中并没有函数重载的概念,但函数的arguments参数能帮助我们模拟重载. arguments并不是真正的数组,但拥有length(参数数目),且能通过数组下标的方式进行访问,例如argument ...
- Android SDK组件:webview笔记
1.安卓手机中内置了一款webkit内核的浏览器,在SDK中封装为WebView组件. 2.该组件可以在自己的应用程序中显示本地或者Internet上的网页,也可以把它当作一个浏览器来时用. 3.We ...
- hadoop 指定 key value分隔符
原文:http://wingmzy.iteye.com/blog/1260570 hadoop中的map-reduce是处理<key,value>这样的键值对,故指定<key,val ...