-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0525 [M] Contiguous Array
Code with Senpai edited this page Apr 7, 2021
·
3 revisions
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
footer