-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy path525_Contiguous_Array.py
More file actions
23 lines (20 loc) · 610 Bytes
/
525_Contiguous_Array.py
File metadata and controls
23 lines (20 loc) · 610 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# https://tinyurl.com/qmbd2vl
class Solution(object):
def findMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count, maxLenSoFar = 0, 0
counterMap = {count: -1} # count:index,
for i in range(len(nums)):
currentNum = nums[i]
if currentNum == 0:
count -= 1
else:
count += 1
if count in counterMap:
maxLenSoFar = max(maxLenSoFar, i - counterMap[count])
else:
counterMap[count] = i
return maxLenSoFar