0525 - Contiguous Array (Medium)
Problem Link
https://leetcode.com/problems/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 <= 10^5
nums[i]
is either0
or1
.
Approach 1: Prefix Sum + Hash Map
Since the number only contain either 0 and 1, we can check the balance and calculate the length. Let be the sum with initial value 0. If is 1, then . If is 0, then . We iterate to calculate . If we get the same at some point, then it means we have one possible answer.
For example, given the input , would be 0 -> -1 -> -2 -> -3 -> -2 -> -1 -> 0. We can see that there are three contiguous subarrays with an equal number of 0 and 1, which are and . The longest contiguous subarray with an equal number of 0 and 1 is which has the length of .
Therefore, the approach is to calculate the prefix sum and put it into a hash map. If the prefix sum can be found, then the we can compare the length with the current maximum answer to see if we update it or not. This solution gives both time complexity and space complexity.
- C++
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int ans = 0, pre = 0, n = nums.size();
unordered_map<int, int> m;
for (int i = 0; i < n; i++) {
pre += 2 * nums[i] - 1;
if (pre == 0) ans = max(ans, i + 1);
if (m.count(pre)) ans = max(ans, i - m[pre]);
else m[pre] = i;
}
return ans;
}
};
Or you can initialise for .
- C++
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int ans = 0, pre = 0, n = nums.size();
unordered_map<int, int> m;
m[0] = -1;
for (int i = 0; i < n; i++) {
pre += 2 * nums[i] - 1;
if (m.count(pre)) ans = max(ans, i - m[pre]);
else m[pre] = i;
}
return ans;
}
};