Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eee7127

Browse files
Create 904.py
1 parent a68dc6c commit eee7127

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎500-1000/904.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def totalFruit(self, tree):
3+
"""
4+
:type tree: List[int]
5+
:rtype: int
6+
"""
7+
j = 0
8+
dic = {}
9+
maxlen = -float('inf')
10+
for i in range(len(tree)):
11+
if tree[i] not in dic:
12+
dic[tree[i]] = 1
13+
else:
14+
dic[tree[i]] += 1
15+
while len(dic) > 2:
16+
dic[tree[j]] -= 1
17+
if dic[tree[j]] == 0:
18+
del dic[tree[j]]
19+
j += 1
20+
maxlen = max(maxlen, i-j+1)
21+
return maxlen if maxlen != -float('inf') else 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /