Contiguous Array (opens in a new tab) Medium
Problem
Given a binary array nums
, find the maximum length of a contiguous subarray with an equal number of 0s and 1s.
Example
Input: nums = [0,1]
Output: 2
Summary
To solve this problem, we can use a hashmap to store the counts of 0s and 1s. As we iterate through the array, we can update the counts and check if we've seen the current count difference before. If we have, then we know we've found a contiguous subarray with an equal number of 0s and 1s. We can keep track of the maximum length of such a subarray and return it at the end.
Solution
In TypeScript
function findMaxLength(nums: number[]): number {
const counts = new Map<number, number>();
counts.set(0, -1);
let maxLen = 0;
let count = 0;
for (let i = 0; i < nums.length; i++) {
count += nums[i] === 1 ? 1 : -1;
if (counts.has(count)) {
maxLen = Math.max(maxLen, i - (counts.get(count) as number));
} else {
counts.set(count, i);
}
}
return maxLen;
}
In Python
def findMaxLength(self, nums: List[int]) -> int:
counts = {0: -1}
max_len = 0
count = 0
for i, num in enumerate(nums):
count += 1 if num == 1 else -1
if count in counts:
max_len = max(max_len, i - counts[count])
else:
counts[count] = i
return max_len
Step-by-step explanation
- Initialize a Map named
counts
to store the count differences as keys and their corresponding indices as values. Set an initial entry with a key of0
and a value of-1
.
counts = {0: -1}
- Initialize two variables:
maxLen
to store the maximum length of a contiguous subarray with equal numbers of 0s and 1s, andcount
to store the current count difference. Set both to 0.
maxLen = 0;
count = 0;
- Iterate through the input array using a for loop.
for (let i = 0; i < nums.length; i++) {
// ...
}
- Update the
count
variable. If the current number is 1, add 1 to the count; otherwise, subtract 1.
count += nums[i] === 1 ? 1 : -1;
- Check if the
counts
Map already contains the currentcount
value as a key. If it does, calculate the length of the contiguous subarray by subtracting the value corresponding to the currentcount
key from the current indexi
, and updatemaxLen
with the maximum of its current value and the calculated length.
if (counts.has(count)) {
maxLen = Math.max(maxLen, i - (counts.get(count) as number));
}
- If the current
count
is not in thecounts
Map, add an entry with the currentcount
as the key and the current indexi
as the value.
else {
counts.set(count, i);
}
- Finally, return the maximum length of the contiguous subarray with equal numbers of 0s and 1s.
return maxLen;
Complexity Analysis
Time Complexity
The time complexity of this solution is O(n), where n is the length of the input array. We iterate through the input array once, updating the counts and checking if we've seen the count difference before.
Space Complexity
The space complexity of this solution is O(n), as we are storing the counts of 0s and 1s in a hashmap. In the worst case, the hashmap will store n entries, where n is the length of the input array.