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_lenclass Solution:
def equalOneZero(n):
return True if n.count(1) == n.count(0) else False
def findMaxLength(self, nums: List[int]) -> int:
max_length = 0
for i in range(len(nums)):
for j in range(i+1,len(nums)+1):
if Solution.equalOneZero(nums[i:j]) and len(nums[i:j])>max_length:
max_length = len(nums[i:j])
return max_length
Last updated
Was this helpful?