Skip to content

Commit 107707a

Browse files
Merge pull request #2862 from Dhyan-P-Shetty/main
Create: 0904-fruit-into-baskets.py
2 parents a34ab06 + 008f4f0 commit 107707a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: python/0904-fruit-into-baskets.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import collections
2+
3+
class Solution:
4+
def totalFruit(self, fruits: List[int]) -> int:
5+
count = collections.defaultdict(int)
6+
l, total, res = 0, 0, 0
7+
8+
for r in range(len(fruits)):
9+
count[fruits[r]] += 1
10+
total += 1
11+
12+
while len(count) > 2:
13+
f = fruits[l]
14+
count[f] -= 1
15+
total -= 1
16+
l += 1
17+
if not count[f]:
18+
count.pop(f)
19+
20+
res = max(res, total)
21+
22+
return res
23+
24+
25+
26+
27+
28+
29+

0 commit comments

Comments
 (0)