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 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的更多相关文章
- [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 904. Fruit Into Baskets
sliding window(滑动窗口)算法 class Solution(object): def totalFruit(self, tree): """ :type ...
- [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 ...
- [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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [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 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- Centos7初始化脚本
今天分享一个自己写的一个初始化的小脚本. 编写初始化系统要考虑到系统的版本问题,现在用的比较多的就是centos6和centos7,所以首先要判断一下系统的版本. cat /etc/redhat-re ...
- C# 说说lock到底锁谁?(2)
摘要 今天在园子里面有园友反馈关于[C#基础]说说lock到底锁谁?文章中lock(this)的问题.后来针对文章中的例子,仔细想了一下,确实不准确,才有了这篇文章的补充,已经对文章中的demo进行修 ...
- C# 关闭子窗体释放子窗体对象问题
1 在主窗口中实例化子窗口 Form2 f2 = new Form2(); 2 通过按钮来显示子窗口 f2.Show(); 3 关闭子窗口而不释放子窗口对象的方法 protected override ...
- 深入探访支付宝双11十年路,技术凿穿焦虑与想象极限 | CYZONE特写
小蚂蚁说: 双11十年间,交易规模的指数级增长不断挑战人们的想象力,而对蚂蚁技术团队来说,这不仅是一场消费盛宴,而是无数次濒临压力和焦虑极限的体验,更是技术的练兵场.如今双11对蚂蚁金服而言,已经绝不 ...
- beyond compare 免费版
链接:https://pan.baidu.com/s/10lPUEpFPZU76HRbJfbZ2ZA 提取码:r2go
- React项目中使用HighCharts
大家都知道BizCharts是基于react封装的一套图表工具,而HighCharts是基于jQuery的.但是由于本人对BizCharts甚是不熟,所以在react项目开发中选择了HighChart ...
- oracle数据库 concat 与 ||
1.简述: 通过几条sql语句来引入此问题 先新建一张表,插入几条数据,如下: CREATE TABLE tab1 (col1 VARCHAR2(6), col2 CHAR(6), col3 VARC ...
- zend cache使用
require_once 'Zend/Cache.php';//引用文件$frontendOptions = array( 'lifeTime' => 60, // cache lifetime ...
- springboot打成Jar包后部署至Linux服务器上
下面主要记录一下springboot打包成jar包在Linux服务上部署的步骤: 1.通过WinSCP,将相应的Jar文件,复制到Linux指定目录下,如/home/ 2.打开ssh,进入/home目 ...
- 羽翼metasploit第一,二季学习笔记
-----------------第一季-------------------- 启动Metasploit:msfconsole 升级和更新:./msfupdate 直接退出:exit 退回上一级:q ...