Contiguous Array
Problem Statement
Given a binary array nums
, return the maximum length of a contiguous subarray with an equal number of 0
and 1
.
Example 1:
Input: nums = [0,1]
Output: 2
Explanation:
[0, 1]
is the longest contiguous subarray with an equal number of 0 and 1.
Example 2:
Input: nums = [0,1,0]
Output: 2
Explanation:
[0, 1]
(or [1, 0]
) is a longest contiguous subarray with equal number of 0 and 1.
Constraints:
1
<=
nums.length
<=
105nums[i]
is either 0 or 1.
Code
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
max_length =0
hash={}
count=0
for i in range(len(nums)):
current=nums[i]
if current==0:
count-=1 # decrement our count if our current element is 0
else:
# increment our count if current element is 1
count+=1
if count==0:
# if count is 0, we have a new subarray with length+1
max_length=i+1
if count in hash:
max_length=max(max_length,i-hash[count])
else:
hash[count]=i
return max_length