Leetcode -- 904. Fruit Into Baskets

更新 發佈閱讀 13 分鐘

將水果放入籃子 - LeetCode --- Fruit Into Baskets - LeetCode

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.

你正在參觀一個農場,農場裡有一排果樹,從左到右排列。這些果樹以一個整數陣列 fruits fruits[i] 表示 i th 棵樹所結的水果種類 。


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.

給定整數數組 fruits ,返回您可以挑選的最大水果數量 。

Solution

There are two method to solve it. Remarkably, one of the method is much faster than the other. So, let's see what's the different between thess two solutions.

這篇文章將提供兩種方法。並且,在學習完這兩種方法後,你會發現其中一種的速度驚人地快很多。所以趕快讓我們進入學習地世界吧!

method 1

Both of thess method use the concept of sliding window. If you are the first time learning this concept, I recommend this video。It has great anime to make learner understand easily.

這兩種方法都用到了 sliding window 的概念。如果是第一次接觸到這個概念,我推薦這部影片。其中包含了清楚的動畫讓使用者可以快速理解 sliding window 的概念。

from collections import defaultdict
class Solution:
def totalFruit(self, fruits: List[int]) -> int:
left = 0
fruit_type = defaultdict(int)
best = 0
for current in range(len(fruits)):
fruit_type[fruits[current]] += 1
while len(fruit_type) > 2:
fruit_type[fruits[left]] -= 1
if fruit_type[fruits[left]] == 0:
del fruit_type[fruits[left]]
left += 1
best = max(best, sum(fruit_type.values()))
return best

If you really understand the concept of sliding window, the code above should be straightforward. The part might be raise question are the variable and the defaultdict in the first line. Let me explain it one by one.

如果有理解 sliding window 的概念,上方的程式碼應該不成問題。可能有疑問的地方是變數和第一行的 defaultdict。讓我逐個解釋。

defaultdict allows user to automatically create a key and asign it a default value when accessing a key that doesn't exist. Unlike a regular dictionary, it won't raise a KeyError when the key is missing. If we don't use defaultdict, the line 8 should add some judgement.

defaultdict允許使用者在存取不存在的鍵時,自動建立該鍵並賦予一個預設值。 不會像普通字典那樣在鍵不存在時拋出 KeyError。如果不使用 defaultdict,第 8 行需要再多一個判斷。

The variable left keeps track the left-end index of the window, making it easier to remove later. The variable fruit_type keeps track of how many types of fruit are currently in the window. A dictionary is used because of its key-value pair structure, and the fact that each key can only appear once, ensuring no duplicates. The variable best is responsible for recording the length of the longest window. The variable current in the for loop represents the current position and the right-end index of the window.

變數 left 記錄著窗戶的左端 index,方便之後刪除。變數 fruit_type 記錄著目前的 窗戶中有幾種水果,而選擇 dictionary 是因為他鍵值對的特性以及不能有重複的 key。變數 best 負責記錄最大窗戶長。for 迴圈的變數 current 代表現在位置以及窗戶最右端的 index。

method 2

This method is faster—by 174 milliseconds. That might not sound like much, but what if I told you that this small boost could elevate you from outperforming just 10% of others to beating over 94% of top performers? Tempting, right? Let's dive in and see how it works.

這個方法比較快,快了 174 ms。174 ms 可能聽起來沒什麼,但如果說這可以讓你從只贏過 10% 的人晉升為超越 94% 的頂尖人才,是否就聽起來心動多了呢?讓我們一起趕快看看吧。

class Solution:
def totalFruit(self, fruits: List[int]) -> int:
best = 0
lasttype, secondtype, currentlength = -1, -1, 0
lastcount = 0
for currenttype in ​fruits:
if secondtype == currenttype or currenttype == lasttype:
currentlength += 1
else :
currentlength = lastcount + 1

if currenttype == lasttype:
lastcount += 1
else:
lastcount = 1
lasttype, secondtype = currenttype, lasttype
best = max(best, currentlength)
return best

I think what sets the second approach apart is that it doesn't use a dictionary to store the types and counts of fruit—instead, it relies on two variables.

我認為第二個方法最不一樣的地方在於它不是用 dictionary 來存水果種類和個數,而是用兩個變數來存。

After understanding the code, I strongly recommend readers try implementing it themselves. You’ll uncover clever design choices that simply can't be fully appreciated by just reading.

在理解完程式碼後,建議讀者一定要自己試試看,因為你將會從中發現他們的一些巧思,這是光用看的所無法體會的。


Hearing that someone give a like to this article will be lucky a week. ><

聽說幫這篇文章案讚的將會好運一整周。❤️

留言
avatar-img
留言分享你的想法!
avatar-img
周濡墨的沙龍
18會員
120內容數
有別於未付費的文章,專題區的文章偏向愛情短篇小說,較有戲劇性,篇幅也會較長;未付費文章會偏向極短篇小說,並且以類似散文的手法寫作
周濡墨的沙龍的其他內容
2025/08/01
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly.....
Thumbnail
2025/08/01
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly.....
Thumbnail
2025/07/30
information link:Two Sum - LeetCode Description:Given an array of integers nums and an integer target, return indices of the two numbers such......
2025/07/30
information link:Two Sum - LeetCode Description:Given an array of integers nums and an integer target, return indices of the two numbers such......
2025/05/06
這篇文章將藉由 2w1h(what、why、how)來介紹如何透過 github 達到更好的團隊合作。 WHAT github 是甚麼 社交媒體有 ig、facebook 等可以分享自己的生活,而 github 則是工程師分享自己 code 或專案的地方。不過,這並不是 github 受歡迎之
Thumbnail
2025/05/06
這篇文章將藉由 2w1h(what、why、how)來介紹如何透過 github 達到更好的團隊合作。 WHAT github 是甚麼 社交媒體有 ig、facebook 等可以分享自己的生活,而 github 則是工程師分享自己 code 或專案的地方。不過,這並不是 github 受歡迎之
Thumbnail
看更多
你可能也想看
Thumbnail
種植水果 我的故鄉位於東西橫貫公路的西邊,那裡的人們常常會爬到梨山的山頂去種植各式各樣的水果。無論是水蜜桃、蘋果還是梨子,每一樣都充滿了自然的甘甜與新鮮。每年到了豐收的季節,我們的家裡就會堆滿各種水果,而其中,蘋果的釀酒過程更是讓我印象深刻。 只能待在家裡 我還在讀小學一年級的時候,年紀小
Thumbnail
種植水果 我的故鄉位於東西橫貫公路的西邊,那裡的人們常常會爬到梨山的山頂去種植各式各樣的水果。無論是水蜜桃、蘋果還是梨子,每一樣都充滿了自然的甘甜與新鮮。每年到了豐收的季節,我們的家裡就會堆滿各種水果,而其中,蘋果的釀酒過程更是讓我印象深刻。 只能待在家裡 我還在讀小學一年級的時候,年紀小
Thumbnail
台灣是水果王國,一年四季都盛產水果,例如春季有桑葚、梅子、李子、枇杷,夏季有西瓜、芒果、荔枝、百香果,秋季有柚子、柿子、蘋果、火龍果,冬天有草莓、柳丁、柑橘、蜜棗,而芭樂、香蕉、番茄、葡萄更是一年四季都生產,今天和大家分享六首以水果為名的歌曲:
Thumbnail
台灣是水果王國,一年四季都盛產水果,例如春季有桑葚、梅子、李子、枇杷,夏季有西瓜、芒果、荔枝、百香果,秋季有柚子、柿子、蘋果、火龍果,冬天有草莓、柳丁、柑橘、蜜棗,而芭樂、香蕉、番茄、葡萄更是一年四季都生產,今天和大家分享六首以水果為名的歌曲:
Thumbnail
早上看到隔壁鄰居在採樹葡萄 原來到了樹葡萄的產季了 樹葡萄真正的名字是「嘉寶果」 嘉寶果又被稱為樹葡萄
Thumbnail
早上看到隔壁鄰居在採樹葡萄 原來到了樹葡萄的產季了 樹葡萄真正的名字是「嘉寶果」 嘉寶果又被稱為樹葡萄
Thumbnail
學會了用手套採摘柳橙 放入了果籃當中安頓它們 從果園去到村莊 一直都十分寧靜 妳們沒有因為好奇心而發問 都沒有再戴上手套了 柳橙還在果籃當中浮動 妳們這次用上了馬車 柳橙的顏色永遠都是一樣 而其實所有的果子都要面對命運 根本像沒法逃跑或避開夕照 它們在某處建立連繫
Thumbnail
學會了用手套採摘柳橙 放入了果籃當中安頓它們 從果園去到村莊 一直都十分寧靜 妳們沒有因為好奇心而發問 都沒有再戴上手套了 柳橙還在果籃當中浮動 妳們這次用上了馬車 柳橙的顏色永遠都是一樣 而其實所有的果子都要面對命運 根本像沒法逃跑或避開夕照 它們在某處建立連繫
Thumbnail
這天,早會結束後,我們帶著孩子們到了後花園散步 ,孩子們發現有一棵樹上結滿了一顆一顆綠綠的果實... 幼兒:那是甚麼? 老師:哪個? 幼兒:綠綠的那個! 老師:我們也不知道耶,你們覺得呢? 幼兒:那個綠綠的!......是棗子! 老師:是嗎? 幼兒:對啦!是棗子!(此
Thumbnail
這天,早會結束後,我們帶著孩子們到了後花園散步 ,孩子們發現有一棵樹上結滿了一顆一顆綠綠的果實... 幼兒:那是甚麼? 老師:哪個? 幼兒:綠綠的那個! 老師:我們也不知道耶,你們覺得呢? 幼兒:那個綠綠的!......是棗子! 老師:是嗎? 幼兒:對啦!是棗子!(此
Thumbnail
題目敘述 題目會給定一個二維陣列grid,代表每顆橘子分布的位置和初始狀態。 0: 這個格子點沒有東西。 1: 這個格子點有一顆新鮮的橘子。 2: 這個格子點有一顆壞掉的橘子。 壞掉的橘子上面的黴菌, 每隔一個週期,可以向上、下、左、右 N4四連通的格子點感染一次。 請問,最少需要多
Thumbnail
題目敘述 題目會給定一個二維陣列grid,代表每顆橘子分布的位置和初始狀態。 0: 這個格子點沒有東西。 1: 這個格子點有一顆新鮮的橘子。 2: 這個格子點有一顆壞掉的橘子。 壞掉的橘子上面的黴菌, 每隔一個週期,可以向上、下、左、右 N4四連通的格子點感染一次。 請問,最少需要多
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News