LC 0525 [M] Contiguous Array - ALawliet/algorithms GitHub Wiki
count is like the cumsum of the max subarray equals k where cumsum = count - 0
note here at i=0 the count is 0 whereas in max subarray at i=0 the cumsum is already > 0 therefore we use a dummy i=-1 is 0
class Solution(object):
def findMaxLength(self, nums):
max_len = 0
count_to_i = {0: 0}
count = 0
for i, num in enumerate(nums, start=1):
if num == 0:
count -= 1
else:
count += 1
if count not in count_to_i:
count_to_i[count] = i
else:
max_len = max(max_len, i - count_to_i[count])
return max_len