Contiguous Array LeetCode solution in Python

1
class Solution:
2
def findMaxLength(self, nums: List[int]) -> int:
3
# dictionary
4
prefixSum = {0: -1}
5
total = 0
6
maxlength = 0
7

8
for index, value in enumerate(nums):
9
if value == 0:
10
total -= 1
11
else:
12
total += 1
13
if total not in prefixSum.keys():
14
prefixSum[total] = index
15
else:
16
maxlength = max(maxlength, index - prefixSum[total])
17
return maxlength

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0