In a row of trees, the i-th tree produces fruit with type tree[i].

You start at any tree of your choice, then repeatedly perform the following steps:

Add one piece of fruit from this tree to your baskets.  If you cannot, stop.
Move to the next tree to the right of the current tree. If there is no tree to the right, stop.
Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. What is the total amount of fruit you can collect with this procedure? Example 1: Input: [1,2,1]
Output: 3
Explanation: We can collect [1,2,1].
Example 2: Input: [0,1,2,2]
Output: 3
Explanation: We can collect [1,2,2].
If we started at the first tree, we would only collect [0, 1].
Example 3: Input: [1,2,3,2,2]
Output: 4
Explanation: We can collect [2,3,2,2].
If we started at the first tree, we would only collect [1, 2].
Example 4: Input: [3,3,3,1,2,1,1,2,3,3,4]
Output: 5
Explanation: We can collect [1,2,1,1,2].
If we started at the first tree or the eighth tree, we would only collect 4 fruits. Note: 1 <= tree.length <= 40000
0 <= tree[i] < tree.length

用 two points 去维护一个 window, 注意一些coner case

class Solution {
public int totalFruit(int[] tree) {
if(tree == null || tree.length == 0){
return 0;
}
int len = tree.length;
int p = 0;
int q = 0;
int max = 0;
Set<Integer> set = new HashSet<>();
while(p < len){
if(set.size() == 0){
set.add(tree[p]);
p++;
}
else if(set.contains(tree[p])){
p++;
}
else{
if(set.size() == 1){
set.add(tree[p]);
p++;
}
else{
if(p - q > max){
max = p - q;
}
set.clear(); int temp = p - 1;
int type = tree[temp];
set.add(tree[p - 1]);
set.add(tree[p]);
while(tree[temp] == type){
temp --;
}
q = temp+1;
}
}
}
if(p - q > max){
max = p - q;
}
return max; }
}

LeetCode - 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 904. Fruit Into Baskets

    sliding window(滑动窗口)算法 class Solution(object): def totalFruit(self, tree): """ :type ...

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

  6. LeetCode Longest Substring with At Most Two Distinct Characters

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

  7. Swift LeetCode 目录 | Catalog

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

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

  9. All LeetCode Questions List 题目汇总

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

随机推荐

  1. JPA使用指南 javax.persistence的注解配置讲解

    转自http://67566894.iteye.com/blog/659829 示例 @SuppressWarnings("serial") @Entity @Table(name ...

  2. CentOS7.3上如何安装Apache/2.4.34

    1)卸载系统自带的httpd Centos可能自带了httpd,但是版本可能会较低,执行下面的命令检测是否已经安装了httpd rpm -qa | grep httpd 如果检测已经安装了二进制的ht ...

  3. Flex外包公司——Flex案例展示

    Flex案例展示 做的mail系统:  http://gowebtop.com/webtop/ 在线购书网站  http://book.orzar.net/ eBay购物网站  http://www. ...

  4. java笔记 -- 数学函数与常量

    Math类中, 包含了各种数学函数 不用在数学方法名和常量名前添加前缀Math., 只要在源文件的顶部加上这行代码: import static java.lang.Math.*; (静态导入) 例: ...

  5. 《Practical Vim》第十章:复制和粘贴

    第十章和第十一章讲了 Vim 的寄存器功能, 寄存器,是用于保存文本的特定的容器.它的内容: 既可以是类似于系统剪切板功能的,用于粘贴的文本 也可以是录制成的宏的命令. 第十章讲使用寄存器使用复制与粘 ...

  6. 连接SQLsever数据库在C#中不能操作的问题

    最近小组成员在用C#连接数据库进行操作的时候,总是注册不起用户,提示为sql.client值不能为NULL,经过了一上午的百度查询,讨论,总是找不到问题所在,不得已去问了老师,老师是专业的软件工程师, ...

  7. 理解SQL的左连接与右连接

    假设有A,B两个表. 表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 a20050114 5 a20050115 表B记录如下: bID ...

  8. Python3+mitmproxy安装使用教程(Windows)

    一.安装 1.1 安装mitmproxy 直接使用pip安装即可 pip install mitmproxy pip本质上会一是安装mitmproxy库的相关代码,二是安装mitmproxy.exe/ ...

  9. commons-lang3工具类学习(二)

    三.BooleanUtils 布尔工具类 and(boolean... array) 逻辑与 BooleanUtils.and(true, true)         = true    Boolea ...

  10. Flutter(一)安装配置和几个注意点

    Flutter(一)安装配置和几个注意点 记住要仰望星空,不要低头看脚下.无论生活如何艰难,请保持一颗好奇心.你总会找到自己的路和属于你的成功. 愿您的来世灵魂依旧,躯体康健. 一句话来形容Flutt ...