525. Contiguous Array

Medium

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Note: The length of the given binary array will not exceed 50,000.

😇 Soltuion

class Solution:
    def findMaxLength(self, nums: List[int]) -> int:
        temp = {}
        max_len = 0
        count = 0
        for i in range(len(nums)):
            count += 1 if nums[i] == 1 else -1
            if(count ==0):
                max_len = max(max_len, i+1)
            if count in temp:
                max_len = max(max_len, i-temp[count])
            else:
                temp[count] = i
        return max_len

Last updated

Was this helpful?