LeetCode #904 Fruit Into Baskets 水果成籃 904 Fruit Into Baskets 水果成籃

904 Fruit Into Baskets 水果成籃

Description:
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces.

You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:

You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
Given the integer array fruits, return the maximum number of fruits you can pick.

Example:

Example 1:

Input: fruits = [1,2,1]
Output: 3
Explanation: We can pick from all 3 trees.

Example 2:

Input: fruits = [0,1,2,2]
Output: 3
Explanation: We can pick from trees [1,2,2].
If we had started at the first tree, we would only pick from trees [0,1].

Example 3:

Input: fruits = [1,2,3,2,2]
Output: 4
Explanation: We can pick from trees [2,3,2,2].
If we had started at the first tree, we would only pick from trees [1,2].

Example 4:

Input: fruits = [3,3,3,1,2,1,1,2,3,3,4]
Output: 5
Explanation: We can pick from trees [1,2,1,1,2].

Constraints:

1 <= fruits.length <= 10^5
0 <= fruits[i] < fruits.length

題目描述:
在一排樹中,第 i 棵樹產生 tree[i] 型的水果。
你可以從你選擇的任何樹開始,然後重複執行以下步驟:

把這棵樹上的水果放進你的籃子裏。如果你做不到,就停下來。
移動到當前樹右側的下一棵樹。如果右邊沒有樹,就停下來。
請注意,在選擇一顆樹後,你沒有任何選擇:你必須執行步驟 1,然後執行步驟 2,然後返回步驟 1,然後執行步驟 2,依此類推,直至停止。

你有兩個籃子,每個籃子可以攜帶任何數量的水果,但你希望每個籃子只攜帶一種類型的水果。

用這個程序你能收集的水果樹的最大總量是多少?

示例 :

示例 1:

輸入:[1,2,1]
輸出:3
解釋:我們可以收集 [1,2,1]。

示例 2:

輸入:[0,1,2,2]
輸出:3
解釋:我們可以收集 [1,2,2]
如果我們從第一棵樹開始,我們將只能收集到 [0, 1]。

示例 3:

輸入:[1,2,3,2,2]
輸出:4
解釋:我們可以收集 [2,3,2,2]
如果我們從第一棵樹開始,我們將只能收集到 [1, 2]。

示例 4:

輸入:[3,3,3,1,2,1,1,2,3,3,4]
輸出:5
解釋:我們可以收集 [1,2,1,1,2]
如果我們從第一棵樹或第八棵樹開始,我們將只能收集到 4 棵水果樹。

提示:

1 <= tree.length <= 40000
0 <= tree[i] < tree.length

思路:

滑動窗口
題目的實際含義是找到一個最多隻含有 2 個元素的滑動窗口的最長長度
滑動窗口裏的元素可以用哈希表或者兩個指針存儲
滑動窗口右邊界向右滑動直到滑動窗口的元素個數多於 2 時, 滑動窗口左邊界向右滑, 直到移除一個元素爲止
時間複雜度爲 O(n), 空間複雜度爲 O(1)

代碼:
C++:

class Solution 
{
public:
    int totalFruit(vector<int>& fruits) 
    {
        int left = 0, right = 0, result = 0, cur = 0, n = fruits.size();
        for (int i = 0; i < n; i++)
        {
            if (fruits[i] != fruits[left] and fruits[i] != fruits[right])
            {
                if (left != right) left = cur;
                right = i;
            }
            result = max(result, i - left + 1);
            if (fruits[cur] != fruits[i]) cur = i;
        }
        return result;
    }
};

Java:

class Solution {
    public int totalFruit(int[] fruits) {
        int left = 0, right = 0, result = 0, cur = 0, n = fruits.length;
        for (int i = 0; i < n; i++)
        {
            if (fruits[i] != fruits[left] && fruits[i] != fruits[right])
            {
                if (left != right) left = cur;
                right = i;
            }
            result = Math.max(result, i - left + 1);
            if (fruits[cur] != fruits[i]) cur = i;
        }
        return result;
    }
}

Python:

class Solution:
    def totalFruit(self, fruits: List[int]) -> int:
        basket, n, result, last = set(), len(fruits), 0, 0
        for i in range(n):
            basket.add(fruits[i])
            if len(basket) > 2:
                last = i - 2
                while fruits[last] == fruits[i - 1]:
                    last -= 1
                basket.remove(fruits[last])
                last += 1
            result = max(result, i - last + 1)
        return result
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章