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 ...
随机推荐
- Learning-Python【16】:模块的导入使用
一.什么是模块 模块就是一系列功能的集合体,一个模块就是一个包含了Python定义和声明的文件,文件名就是模块名字加上.py的后缀. 模块有三种来源: 1.内置的模块 2.第三方的模块 3.自定义模块 ...
- $set()的正确使用方式
vue给对象新增属性,并触发视图更新 如下代码:给student对象新增age属性 data () { return { student: { name: '', sex: '' } } } 众所周知 ...
- js parseInt
语法: parseInt(string, radix); string 要被解析的值.如果参数不是一个字符串,则将其转换为字符串(使用 ToString 抽象操作).字符串开头的空白符将会被忽略. ...
- 20175317 《Java程序设计》第二周学习总结
20175317 <Java程序设计>第二周学习总结 教材学习内容总结 第二周我学习了教材三四章的内容,了解了Java与C语言的相似与不同之处. 其中第二章学到了标识符与关键字.基本数据类 ...
- ES6中export与export default的区别
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...
- FTP:500 OOPS: failed to open vsftpd log file:/var/log/vsftpd.log
如下:从10.12.8.165 FTP 到 10.1.3.34,报failed to open vsftpd log[a4_csbdc@localhost ~]$ ftp 10.1.3.34Conn ...
- 微信小程序onLaunch修改globalData的值
//app.js App({ onLaunch: function (options) { //设置场景值到缓存中: //wx.setStorageSync("scene", op ...
- fastTime从后台传过来显示格式的处理
1.可以在控制台用syso查看 QuestionFeedbackView qfv = on_LineManageService.getBringupHead(projectNo);//qfv是一个内含 ...
- vs开发/IIS 遇到的问题--------笔记
1.VS2017专业版和企业版激活密钥 Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB- ...
- 在myeclipse中使用./和../遇到的问题
今天用ajax验证的时候,ajax的代码一直不起作用,我在浏览器里打开了开发者模式,错误的原因是找不到"jquery-1.8.3.min.js",但是我的目录结构都没有问题. &l ...