Skip to content

Commit 3d2a1a1

Browse files
Refactor docstring in FruitIntoBaskets class
Updated the class docstring to provide a clearer problem statement and example usage.
1 parent 6da352f commit 3d2a1a1

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
"""
2-
Question:
3-
Given an array fruits representing types of fruit, pick a contiguous subarray containing at most 2 different types of fruit.
4-
Return the maximum number of fruits you can collect.
5-
6-
Example:
7-
Input: fruits = [1,2,1,2,3]
8-
Output: 4
9-
Explanation:
10-
Pick subarray [1,2,1,2] -> contains 2 types and length 4
11-
"""
12-
131
from typing import List
142
from collections import defaultdict
153

16-
174
class FruitIntoBaskets:
5+
"""
6+
Problem:
7+
Given an array of integers representing types of fruit, pick a contiguous subarray
8+
containing at most two different types of fruit. Return the maximum number
9+
of fruits you can collect.
10+
11+
Example:
12+
>>> solver = FruitIntoBaskets()
13+
>>> solver.total_fruit([1, 2, 1, 2, 3])
14+
4
15+
"""
16+
1817
def total_fruit(self, fruits: List[int]) -> int:
19-
count = defaultdict(int) # Stores count of each fruit type
20-
left = 0 # Left pointer of sliding window
21-
max_fruit = 0 # Tracks max number of fruits collected
18+
count = defaultdict(int)
19+
left = 0
20+
max_fruit = 0
2221

2322
for right, fruit in enumerate(fruits):
24-
count[fruit] += 1 # Include current fruit
23+
count[fruit] += 1
2524

26-
# Shrink window if more than 2 types
2725
while len(count) > 2:
2826
count[fruits[left]] -= 1
2927
if count[fruits[left]] == 0:
@@ -35,8 +33,7 @@ def total_fruit(self, fruits: List[int]) -> int:
3533
return max_fruit
3634

3735

38-
# Example dry run
3936
if __name__ == "__main__":
40-
fruits = [1, 2, 1, 2, 3]
4137
solver = FruitIntoBaskets()
42-
print("Maximum Fruits Collected:", solver.total_fruit(fruits))
38+
print("Maximum Fruits Collected:", solver.total_fruit([1, 2, 1, 2, 3]))
39+

0 commit comments

Comments
 (0)