Skip to content

Commit cdd410c

Browse files
committed
Aug 04
1 parent 93d2dc2 commit cdd410c

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def totalFruit(self, fruits: list[int]) -> int:
6+
fruit_types = Counter()
7+
distinct, max_fruits = 0, 0
8+
9+
left, right = 0, 0
10+
while right < len(fruits):
11+
if fruit_types[fruits[right]] == 0:
12+
distinct += 1
13+
fruit_types[fruits[right]] += 1
14+
15+
while distinct > 2:
16+
fruit_types[fruits[left]] -= 1
17+
if fruit_types[fruits[left]] == 0:
18+
distinct -= 1
19+
left += 1
20+
21+
max_fruits = max(max_fruits, right - left + 1)
22+
right += 1
23+
24+
return max_fruits
25+
26+
27+
def main():
28+
fruits = [1, 2, 1]
29+
assert Solution().totalFruit(fruits) == 3
30+
31+
fruits = [0, 1, 2, 2]
32+
assert Solution().totalFruit(fruits) == 3
33+
34+
fruits = [1, 2, 3, 2, 2]
35+
assert Solution().totalFruit(fruits) == 4
36+
37+
38+
if __name__ == '__main__':
39+
main()

2025-08-August-LeetCoding-Challenge/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
| --- | --- | --- | --- |
77
| August 01 | [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | Easy | Solved |
88
| August 02 | [2561. Rearranging Fruits](https://leetcode.com/problems/rearranging-fruits/) | Hard | Solved |
9-
| August 03 | []() | | |
10-
| August 04 | []() | | |
9+
| August 03 | [2106. Maximum Fruits Harvested After at Most K Steps](https://leetcode.com/problems/maximum-fruits-harvested-after-at-most-k-steps/) | Hard | Unsolved |
10+
| August 04 | [904. Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | Medium | Solved |
1111
| August 05 | []() | | |
1212
| August 06 | []() | | |
1313
| August 07 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 1 | 1 | 0 |
44-
| Medium | 0 | 0 | 0 |
45-
| Hard | 1 | 1 | 0 |
44+
| Medium | 1 | 1 | 0 |
45+
| Hard | 2 | 1 | 1 |

0 commit comments

Comments
 (0)