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. gets() 与 scanf() 的小尴尬

    gets() 与 scanf() 函数相处呢有点小尴尬的,就是 gets() 在 scanf() 后边就爱捣乱.为什么呢,先了解它们两者之间的异同: 同: 都是可以接受连续的字符数据 并在字符结束后自 ...

  2. Qt获取选择的文件夹和文件路径

    获取文件夹路径 static QString getExistingDirectory(QWidget *parent = Q_NULLPTR, const QString &caption ...

  3. 【Core】.NET Core中读取App.config配置文件

    1.项目中添加App.config文件 因为.NET Core的项目本质是控制台应用,所以ConfigurationManager的API会去默认读取app.config配置文件,而不是web.con ...

  4. Python自学:第二章 使用函数str( )避免类型错误

    age = 23 message = "Happy " + str(age) + "rd Birthday" print(message) 输出位 Happy ...

  5. yii2restful规范的api使用

    说明:restful是一套优秀的接口调用规范. 使用规范: 1,安装yii2 使用composer安装 安装完 Composer,运行下面的命令来安装 Composer Asset 插件: php c ...

  6. Swapping Characters CodeForces - 903E (字符串模拟)

    大意: 给定k个字符串, 长度均为n, 求是否存在一个串S, 使得k个字符串都可以由S恰好交换两个字符得到. 暴力枚举交换的两个字符的位置, 计算出交换后与其他串不同字符的个数, 若为1或>2显 ...

  7. springboot整合mybatis遇到无法扫描MaperScan包的问题

    1.启动类加上@MaperScan注解后,一直报错如下: Error creating bean with name 'platUserMapper' defined in file [D:\work ...

  8. SQL Update 语句详解

    SQL Update 语句详解   Update 语句 Update 语句用于修改表中的数据. 语法: UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 Person: L ...

  9. php intval 两位小数乘以100后结果少1

    价格处理的时候往往是两位小数需要换算成分,如:16.33元换算为1633分,直接乘以100也就行了的,但是又使用了一个转换为整数类型的函数intval() 这下子结果就不对了,如图:  结果:  可以 ...

  10. Android开发 ---基本UI组件7 :分页功能、适配器、滚动条监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...