作者: 负雪明烛
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:

  1. Add one piece of fruit from this tree to your baskets. If you cannot, stop.
  2. 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. 1 <= tree.length <= 40000
  2. 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)的更多相关文章

  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】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. Leetcode 904. Fruit Into Baskets

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

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. Vue.js知识点总结

    1. Vue.js简介 1.1 Vue.js简介 1.2 创建一个vue实例 2. Vue.js基础 2.1 模板语法 2.2 环境搭建 2.3 生命周期钩子

  2. LearnPython_week4

    1.装饰器2.生成器3.迭代器4.内置方法5.可序列化6.项目规范化 1.装饰器 # -*- coding:utf-8 -*- # Author:Wong Du ### 原代码 def home(): ...

  3. SELECT的语法

    我们先回顾下正则表达式.下图: 描述像xy, xxy (B上转一圈), xyy, xxyy这样的字符串.然后可以进行字符串匹配.设计芯片都用Verilog语言而不是画门电路了.像x+y+这样的叫做re ...

  4. COAP协议 - arduino ESP32 M2M(端对端)通讯与代码详解

    前言 最近我在研究 COAP 协议,在尝试使用 COAP 协议找了到了一个能在ESP32上用的coap-simple库,虽然库并不完善关于loop处理的部分应该是没写完,但是对于第一次接触COAP的朋 ...

  5. 论文解读(GraRep)《GraRep: Learning Graph Representations with Global Structural Information》

    论文题目:<GraRep: Learning Graph Representations with Global Structural Information>发表时间:  CIKM论文作 ...

  6. windows磁盘扩容

    要邻近的磁盘,才可以扩展.所以必须要先删除恢复分区. 删除恢复分区,参考如下: https://jingyan.baidu.com/article/574c5219598d5e6c8c9dc15e.h ...

  7. 淘宝、网易移动端 px 转换 rem 原理,Vue-cli 实现 px 转换 rem

       在过去的一段时间里面一直在使用Vue配合 lib-flexible和px2rem-loader配合做移动端的网页适配.秉着求知的思想,今天决定对他的原理进行分析.目前网上比较主流使用的就是淘宝方 ...

  8. 三维引擎导入obj模型全黑总结

    最近有客户试用我们的三维平台,在导入模型的时候,会出现模型全黑和不可见的情况.本文说下全黑的情况. 经过测试,发现可能有如下几种情况. obj 模型没有法线向量 如果obj模型导出的时候没有导出法线向 ...

  9. Lock锁的使用

    在Java多线程中,可以使用synchronized关键字实现线程之间的同步互斥,在jdk1.5后新增的ReentrantLock类同样可达到此效果,且在使用上比synchronized更加灵活. 观 ...

  10. SpringMVC注解详情

    @Component.@Repository @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller 控制层,就是我们的action层 @Service ...