Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ae6534

Browse files
authored
Create 0525-contiguous-array.py
1 parent c3e2c27 commit 9ae6534

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

python/0525-contiguous-array.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def findMaxLength(self, nums: List[int]) -> int:
3+
zero, one = 0, 0
4+
res = 0
5+
6+
diff_index = {}
7+
8+
for i, n in enumerate(nums):
9+
if n == 0:
10+
zero += 1
11+
else:
12+
one += 1
13+
if one - zero not in diff_index:
14+
diff_index[one - zero] = i
15+
16+
if one == zero:
17+
res = one + zero
18+
else:
19+
idx = diff_index[one - zero]
20+
res = max(res, i - idx)
21+
return res

0 commit comments

Comments
 (0)