|
|
|
@@ -0,0 +1,30 @@ |
|
|
|
class Solution: |
|
|
|
def findMaxLength(self, nums): |
|
|
|
result = [] |
|
|
|
current = 0 |
|
|
|
for n in nums: |
|
|
|
if n == 0: |
|
|
|
current -= 1 |
|
|
|
else: |
|
|
|
current += 1 |
|
|
|
result.append(current) |
|
|
|
|
|
|
|
hm = dict() |
|
|
|
hm[0] = [-1, None] |
|
|
|
for ind, val in enumerate(result): |
|
|
|
if val in hm: |
|
|
|
hm[val][1] = ind |
|
|
|
else: |
|
|
|
hm[val] = [ind, None] |
|
|
|
|
|
|
|
maxLength = 0 |
|
|
|
for key, value in hm.items(): |
|
|
|
if value[1] is not None: |
|
|
|
new = value[1] - value[0] |
|
|
|
if new > maxLength: |
|
|
|
maxLength = new |
|
|
|
return maxLength |
|
|
|
|
|
|
|
s = Solution() |
|
|
|
print("Expected: 2") |
|
|
|
print("Got:", s.findMaxLength([1, 0])) |