[LeetCode]Fruit Into Baskets

栏目: 编程工具 · 发布时间: 7年前

内容简介:In a row of trees, theYouNote 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.

原题

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 <= tree.length <= 40000
0 <= tree[i] < tree.length

题解

该题属于双指针(Two Pointers)范畴,主要的思路是利用两个指针来构造一个滑动窗口,且要求窗口中的元素类型不得超过2种。所以,经过一遍for循环,即可拿到所有不超过两个类型元素的窗口长度,即拿到其中最大的长度。

所以,难度在于怎么来构造这个窗口。这里,我们可以巧妙的利用映射(Map)这种数据结构,用映射来维护for循环过程中,经过的数据类型总数,以及每种类型的个数。

以原题中第一个案例为例,在第一次for循环中,我们将第一个元素加入到map中,如下:

map m;

m.insert(make_pair(1, 1)); // 第一个元素代表类型为1,第二个元素代表类型为1的出现的个数

同时,可以很容易的拿到某时刻的状态信息:

m.size(); // 当前不同类型的总数

m.find(1)->second; // 类型为1的出现的次数

代码

class Solution {
    public:
        int totalFruit(vector<int>& tree) {
            map<int, int> m;
            map<int, int>::iterator it;
            int l = 0, r = 0, len = tree.size(), a = 0;
            while (r < len) {
                if (m.size() <= 2) {
                    it = m.find(tree[r]);
                    if (it != m.end()) {
                        it->second++;
                        m.insert(make_pair(tree[r], it->second + 1));
                    } else {
                        m.insert(make_pair(tree[r], 1));
                    }
                    r++;
                    if (a < r - l) a = r - l;
                } else {
                    it = m.find(tree[l]);
                    m.erase(tree[l]);
                    if (it->second != 1) {
                        m.erase(tree[l]);
                        m.insert(make_pair(tree[l], it->second - 1));
                        it = m.find(tree[l]);
                    }
                    l++; 
                }
            }
            return a;
        }
};

原题: https://leetcode.com/problems/fruit-into-baskets/description/

Github 源码: https://github.com/GenialX/leetcode-cpp/blob/master/2018/09/fruit-into-baskets.cpp

文章来源: 胡小旭 =>  [LeetCode]Fruit Into Baskets


以上所述就是小编给大家介绍的《[LeetCode]Fruit Into Baskets》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法帝国

算法帝国

克里斯托弗•斯坦纳 / 李筱莹 / 人民邮电出版社 / 2014-6 / 49.00

人类正在步入与机器共存的科幻世界?看《纽约时报》畅销书作者讲述算法和机器学习技术如何悄然接管人类社会,带我们走进一个算法统治的世界。 今天,算法涉足的领域已经远远超出了其创造者的预期。特别是进入信息时代以后,算法的应用涵盖金融、医疗、法律、体育、娱乐、外交、文化、国家安全等诸多方面,显现出源于人类而又超乎人类的强大威力。本书是《纽约时报》畅销书作者的又一力作,通过一个又一个引人入胜的故事,向......一起来看看 《算法帝国》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具