-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path904.py
32 lines (25 loc) · 938 Bytes
/
904.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
# Name: Fruit Into Baskets
# Link: https://leetcode.com/problems/fruit-into-baskets/
# Method: Keep track of start of seq of prev element (if a third seen, restart from there)
# Time: O(n)
# Space: O(1)
# Difficulty: Medium
class Solution:
def totalFruit(self, tree: List[int]) -> int:
current_fruits = {}
prev = tree[0]
start = 0
max_collect = 0
for i, fruit in enumerate(tree):
if len(current_fruits) < 2:
current_fruits[fruit] = i
elif fruit in current_fruits:
if fruit != prev:
current_fruits[fruit] = i
else:
max_collect = max(max_collect, i - start)
start = current_fruits[prev]
current_fruits = {fruit: i, prev: current_fruits[prev]}
prev = fruit
max_collect = max(max_collect, len(tree) - start)
return max_collect