【LeetCode】904. Fruit Into Baskets 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/fruit-into-baskets/description/
题目描述:
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
题目大意
输入是一排树,每棵树上结的有果子,这个数字代表果子的种类(注意,不是数目)。让你从某个位置开始向右连续的去摘果子,只有两个篮子,每个篮子只能放同一类果子。如果向右遍历的过程中没有果子可以摘了,或者果篮里没法放当前树的果子,那么就停止,问总的能摘多少果子。
解题方法
现在LeetCode就喜欢出一个情景题,让人花了很长时间理解题意,并且抽象出来。这个题如果抽象成模型的话就是,求一个数组的最长连续子数组,要求这个子数组中最多只存在两个不同的元素。
如果做了上面的抽象,那么我们就很容易想到使用双指针,计算双指针区间内的所有元素的个数,这个个数就是我们要求的能摘取的果子个数。同时在移动的过程中要保证,双指针区间内的元素种类最多为2.之前做题的时候有使用Counter直接数一个区间内所有的个数,这样会超时的。所以使用了字典进行存储,每次只更新最右边和最左边的元素的个数即可。
时间复杂度是O(N),空间复杂度是O(N).
class Solution(object):
def totalFruit(self, tree):
"""
:type tree: List[int]
:rtype: int
"""
left, right = 0, 0
res = 0
cnt = collections.defaultdict(int)
while right < len(tree):
cnt[tree[right]] += 1
while len(cnt) > 2:
cnt[tree[left]] -= 1
if cnt[tree[left]] == 0:
del cnt[tree[left]]
left += 1
res = max(res, right - left + 1)
right += 1
return res
参考资料:
https://blog.csdn.net/XX_123_1_RJ/article/details/82828570
日期
2018 年 9 月 30 日 —— 9月最后一天啦!
【LeetCode】904. Fruit Into Baskets 解题报告(Python)的更多相关文章
- [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】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- Leetcode 904. Fruit Into Baskets
sliding window(滑动窗口)算法 class Solution(object): def totalFruit(self, tree): """ :type ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- rust shadow
1 fn main() { 2 let mut demo = 12; 3 println!("{}",demo); 4 demo = 21; // 值可变,数据类型不可变 5 pr ...
- 在Kubernetes上安装Percona XtraDB集群
官方文档地址:https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html 一.简介 Percona XtraDB C ...
- 非寻常方式学习ApacheTomcat架构及10.0.12源码编译
概述 开启博客分享已近三个月,感谢所有花时间精力和小编一路学习和成长的伙伴们,有你们的支持,我们继续再接再厉 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Tomcat官 ...
- 学习java 7.7
学习内容: 多态转型:向上转型 Animal a = new Cat(); a.eat(); 向下转型 Cat c = (Cat)a; c.eat(); 抽象方法没有方法体,抽象类中有抽象方法 抽象类 ...
- linux安装redis报错
问题:You need tcl 8.5 or newer in order to run the Redis test 解决办法: wget http://downloads.sourceforge. ...
- JVM——对象已“死”的判定
主要针对Java堆和方法区 1.判断对象是否已"死" Java堆中存放着几乎所有的对象实例,垃圾回收器在对堆进行回收之前,首先应该判断这些对象哪些还"存活",哪 ...
- Nginx 1.9.7.2 + PHP 5.6.18(FastCGI)在CentOS Linux下的编译安装
本文参考张宴的Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]完成.所有操作命令都在CentOS 6.x 64位操作系统下实践 ...
- 通过 Ajax 发送 PUT、DELETE 请求的两种实现方式
一.普通请求方法发送 PUT 请求 1. 如果不用 ajax 发送 PUT,我们可以通过设置一个隐藏域设置 _method 的值,如下: <form action="/emps&quo ...
- java中的迭代器的含义
可迭代是Java集合框架下的所有集合类的一种共性,也就是把集合中的所有元素遍历一遍.迭代的过程需要依赖一个迭代器对象,那么什么是迭代器呢? 迭代器(Iterator)模式,又叫做游标模式,它的含义是, ...
- nvm命令
1.安装node nvm install node版本 2.查看已安装版本 nvm list 3.切换node版本 nvm use node版本 4.查看版本 node -v