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的更多相关文章

  1. [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 ...

  2. 【LeetCode】904. Fruit Into Baskets 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/fruit-in ...

  3. 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 ...

  4. [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 ...

  5. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. [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 ...

  8. LeetCode Longest Substring with At Most Two Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...

  9. [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 ...

随机推荐

  1. Android:日常学习笔记(2)——分析第一个Android应用程序

    Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...

  2. Loadrunder场景设计篇——手工场景设计

    概述 通过选择需要运行的脚本,分配运行脚本的负载生成器,在脚本中分配Vuser来建立手工场景 手工场景就是自行设置虚拟用户的变化,主要是通过设计用户的添加和减少过程,来模拟真实的用户请求模型,完成负载 ...

  3. sql备份命令

    --将SQL脚本赋值给变量 ) set @SqlBackupDataBase=N'BACKUP DATABASE dbname TO DISK = ''E:\DBBackup\dbname-'+ ), ...

  4. usb2.0 协议分析

    转:https://blog.csdn.net/u011594613/article/details/48291307 一.USB硬件介绍1.1.概述 一条USB传输线分别由地线.电源线.D+和D-四 ...

  5. java配置好jdk-bash: /usr/bin/java: No such file or directory

    在 Linux 系统中安装 JDK 环境,配置好环境变量后,输入 java.javac 或者 java -version 等时,都提示如下错误: -bash: /usr/local/java/bin/ ...

  6. java-jpa-criteriaBuilder使用入门

    项目中使用jpa ,第一次见查询起来一脸蒙,这就去查下jpa查询的方式,和概念. jpa 元模型 criteria 查询 CriteriaBuilder 安全查询创建工厂 CriteriaQuery ...

  7. 数据库系统概论学习2-《关系数据库与 E/R 模型》

    -----------------------------一直更新学习内容------------------------------------ 建立一个关系数据库需要几步? 2.关系数据库与 E/ ...

  8. groupby和agg的使用

    先来看一段代码: 分析下groupby和agg的联合使用: reset_index()表示重新设置索引 agg传进来的统计特征: 按照A这一列作聚合,C这一列作统计 注意:df = df.groupb ...

  9. Device Tree(一):背景介绍【转】

    本文转载自:http://www.wowotech.net/device_model/why-dt.html 一.前言 作为一个多年耕耘在linux 2.6.23内核的开发者,各个不同项目中各种不同周 ...

  10. Spark Shuffle的技术演进

      在Spark或Hadoop MapReduce的分布式计算框架中,数据被按照key分成一块一块的分区,打散分布在集群中各个节点的物理存储或内存空间中,每个计算任务一次处理一个分区,但map端和re ...