-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0904-fruit-into-baskets.py
37 lines (33 loc) · 1 KB
/
0904-fruit-into-baskets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# time complexity: O(n)
# space complexity: O(n)
from typing import List
class Solution:
def totalFruit(self, fruits: List[int]) -> int:
left = 0
total = 0
freq = {}
result = float("-inf")
for right in range(len(fruits)):
currFruit = fruits[right]
if currFruit not in freq:
freq[currFruit] = 1
else:
freq[currFruit] += 1
total += 1
while len(freq) > 2:
prevFruit = fruits[left]
freq[prevFruit] -= 1
total -= 1
left += 1
if freq[prevFruit] == 0:
del freq[prevFruit]
result = max(result, total)
return result
fruits = [1, 2, 1]
print(Solution().totalFruit(fruits))
fruits = [0, 1, 2, 2]
print(Solution().totalFruit(fruits))
fruits = [1, 2, 3, 2, 2]
print(Solution().totalFruit(fruits))
fruits = [3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4]
print(Solution().totalFruit(fruits))