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 ...
随机推荐
- OSI参考模型与排错
OSI参考模型中底层为其上层服务,因此排除网络故障应该也从底层到高层依次排查. 首先检查网络连接是否正常(物理层检查).例如网卡没有接好网线,将会看到带红叉的本地连接,属于物理层故障. 连接好网卡后, ...
- Android:日常学习笔记(8)———探究UI开发(2)
Android:日常学习笔记(8)———探究UI开发(2) 对话框 说明: 对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 提示 ...
- 每天一个Linux命令(59)wget命令
wget命令用来从指定的URL下载文件. (1)用法: 用法: wget [参数] [URL] (2)功能: 功能: wget命令用来从指定的URL下载 ...
- Django---view视图FBV&CBV
一:创建项目和应用: 或者用命令创建: 1:django-admin.py startproject CBV&FBV 2: cd CBV&FBV (路径切到该文件夹下) 3: pyth ...
- CSS3 3D旋转下拉菜单
在线演示 本地下载
- 20145240 《Java程序设计》第二次实验报告
20145240 <Java程序设计>第二次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.12 实验 ...
- 创建Android守护进程(底层服务)【转】
本文转载自:https://blog.csdn.net/myfriend0/article/details/80016739 创建Android守护进程(底层服务) 前言 Android底层服务,即运 ...
- 自动生成Mapper代码
public class BeanMapperTest { @Test public void build() throws Exception { Class clazz = RiskAccess. ...
- idea创建git分支
此时只是在本地创建好了分支,修改源代码后add,commit将本地分支提交到远程仓库 分支已创建,其它成员此时就可以从git拉分支
- java resources 红叉 An error occurred while filtering resources
用eclipse创建了一个Spring mvc的Maven项目,在项目上有个叉叉,通过Window -> Show View -> Markers中看到错误原因 An error occu ...