https://leetcode.com/problems/contiguous-array
X.
https://discuss.leetcode.com/topic/80020/one-pass-use-a-hashmap-to-record-0-1-count-difference
https://discuss.leetcode.com/topic/79906/easy-java-o-n-solution-presum-hashmap
https://discuss.leetcode.com/topic/80056/python-o-n-solution-with-visual-explanation/2
https://discuss.leetcode.com/topic/79907/share-my-dp-map-solution-one-pass/2
Use dp[][] to store the zeros and ones from start to current position
and use map to store the difference of ones and zeros with index;
if current position ones-zeros = 2, we check map if there is other position ones-zeros=2
if we find, the 2*(current index - that index) is candidate
the max candidate is the answer
X.
https://leetcode.com/articles/contiguous-array/
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.
Example 1:
Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.
Example 2:
Input: [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
Note: The length of the given binary array will not exceed 50,000.
X.
https://discuss.leetcode.com/topic/80020/one-pass-use-a-hashmap-to-record-0-1-count-difference
public int findMaxLength(int[] nums) {
HashMap<Integer,Integer> map=new HashMap<>();
map.put(0,-1);
int zero=0;
int one=0;
int len=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
zero++;
}else{
one++;
}
if(map.containsKey(zero-one)){
len=Math.max(len,i-map.get(zero-one));
}else{
map.put(zero-one,i);
}
}
return len;
}
https://leetcode.com/articles/contiguous-array/public int findMaxLength(int[] nums) { Map<Integer, Integer> map = new HashMap<>(); map.put(0, -1); int maxlen = 0, count = 0; for (int i = 0; i < nums.length; i++) { count = count + (nums[i] == 1 ? 1 : -1); if (map.containsKey(count)) { maxlen = Math.max(maxlen, i - map.get(count)); } else { map.put(count, i); } } return maxlen; }
https://discuss.leetcode.com/topic/79906/easy-java-o-n-solution-presum-hashmap
The idea is to change
0
in the original array to -1
. Thus, if we find SUM[i, j] == 0
then we know there are even number of -1
and 1
between index i
and j
. Also put the sum
to index
mapping to a HashMap to make search faster. public int findMaxLength(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) nums[i] = -1;
}
Map<Integer, Integer> sumToIndex = new HashMap<>();
sumToIndex.put(0, -1);
int sum = 0, max = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (sumToIndex.containsKey(sum)) {
max = Math.max(max, i - sumToIndex.get(sum));
}
else {
sumToIndex.put(sum, i);
}
}
return max;
}
I had a similar idea, but in one pass:
public int findMaxLength(int[] nums) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{put(0,0);}};
int maxLength = 0, runningSum = 0;
for (int i=0;i<nums.length;i++) {
runningSum += nums[i];
Integer prev = map.get(2*runningSum-i-1);//?
if (prev != null) maxLength = Math.max(maxLength, i+1-prev);
else map.put(2*runningSum-i-1, i+1);
}
return maxLength;
}
https://discuss.leetcode.com/topic/79977/python-and-java-with-little-tricks-incl-a-oneliner/2
Using
putIfAbsent
so I only need one map function call per number.public int findMaxLength(int[] nums) {
Map<Integer, Integer> index = new HashMap<>();
index.put(0, -1);
int balance = 0, maxlen = 0;
for (int i = 0; i < nums.length; i++) {
balance += nums[i] * 2 - 1;
Integer first = index.putIfAbsent(balance, i);
if (first != null)
maxlen = Math.max(maxlen, i - first);
}
return maxlen;
}
Could avoid using
Math.max
like this: if (first != null && i - first > maxlen)
maxlen = i - first;
https://discuss.leetcode.com/topic/79932/java-one-pass-o-n-solution-with-explanationhttps://discuss.leetcode.com/topic/80056/python-o-n-solution-with-visual-explanation/2
def findMaxLength(self, nums):
count = 0
max_length=0
table = {0: 0}
for index, num in enumerate(nums, 1):
if num == 0:
count -= 1
else:
count += 1
if count in table:
max_length = max(max_length, index - table[count])
else:
table[count] = index
return max_length
X.https://discuss.leetcode.com/topic/79907/share-my-dp-map-solution-one-pass/2
Use dp[][] to store the zeros and ones from start to current position
and use map to store the difference of ones and zeros with index;
if current position ones-zeros = 2, we check map if there is other position ones-zeros=2
if we find, the 2*(current index - that index) is candidate
the max candidate is the answer
public int findMaxLength(int[] nums) {
int n = nums.length, res = 0;
Map<Integer, Integer> map = new HashMap<>();
int[][] dp = new int[n+1][2];
for (int i = 1; i < dp.length; i++) {
if (nums[i-1] == 0) {
dp[i][0] = dp[i-1][0]+1;
dp[i][1] = dp[i-1][1];
}else {
dp[i][0] = dp[i-1][0];
dp[i][1] = dp[i-1][1]+1;
}
if (dp[i][0] == dp[i][1]) res = Math.max(res, dp[i][0]*2);
else {
int dif = dp[i][1]-dp[i][0];
if (map.containsKey(dif)) res = Math.max(res, 2*(dp[i][0]-dp[map.get(dif)][0]));
else map.put(dif, i);
}
}
return res;
}
X.
https://leetcode.com/articles/contiguous-array/
The brute force approach is really simple. We consider every possible subarray within the given array and count the number of zeros and ones in each subarray. Then, we find out the maximum size subarray with equal no. of zeros and ones out of them.
public int findMaxLength(int[] nums) { int maxlen = 0; for (int start = 0; start < nums.length; start++) { int zeroes = 0, ones = 0; for (int end = start; end < nums.length; end++) { if (nums[end] == 0) { zeroes++; } else { ones++; } if (zeroes == ones) { maxlen = Math.max(maxlen, end - start + 1); } } } return maxlen; }