/**
Max Consecutive Ones II
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.
Example 1:
Input: [1,0,1,1,0]
Output: 4
Explanation: Flip the first zero will get the the maximum number of consecutive 1s.
After flipping, the maximum number of consecutive 1s is 4.
Note:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
Follow up: What if the input numbers come in one by one as an infinite stream? In other words, you can't store all numbers coming from the stream as it's too large to hold in memory. Could you solve it efficiently?
*/
import java.util.*;
public class MaxConsecutiveOnesII{
/**
-keep track of the number of zeros
-set one pointer to the left, and another pointer to iterate the elements
-when zero count reaches 2, repeatedly check the left pointer elements for 1,. This brings the left pointer to the next occurence of 0
-the right pointer - left pointer will give the window with max consecutive ones, when we add 1, this will fill the gap by flipping a zero to 1
*/
public static int findMaxConsecutiveOnes(int[] nums) {
int zeros = 0, max = 0;
for(int l =0, r =0; r<nums.length; r++){
if(nums[r]==0) zeros++;
if(zeros==2){
while(nums[l]==1){l++;}
l++;
zeros--;
}
max = Math.max(max, r-l+1);
System.out.println(l+"..."+r+"..."+max);
}
return max;
}
/**
Followup:
What if the input numbers come in one by one as an infinite stream? In other words, you can't store all numbers coming from the stream as it's too large to hold in memory. Could you solve it efficiently?
*/
public static int findMaxConsecutiveOnesInStream(int[] nums) {
int max = 0, k = 1; // flip at most k zero
Queue<Integer> zeroIndex = new LinkedList<>();
for (int l = 0, r = 0; r < nums.length; r++) {
if (nums[r] == 0)
zeroIndex.offer(r);
if (zeroIndex.size() > k)
l = zeroIndex.poll() + 1;
max = Math.max(max, r - l + 1);
}
return max;
}
public static void main(String[] args){
System.out.println(findMaxConsecutiveOnes(new int[]{1,0,1,1,0}));//4
System.out.println(findMaxConsecutiveOnes(new int[]{1,1,1,0,1,1,0}));//6
}
}
Max Consecutive Ones II
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.
Example 1:
Input: [1,0,1,1,0]
Output: 4
Explanation: Flip the first zero will get the the maximum number of consecutive 1s.
After flipping, the maximum number of consecutive 1s is 4.
Note:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
Follow up: What if the input numbers come in one by one as an infinite stream? In other words, you can't store all numbers coming from the stream as it's too large to hold in memory. Could you solve it efficiently?
*/
import java.util.*;
public class MaxConsecutiveOnesII{
/**
-keep track of the number of zeros
-set one pointer to the left, and another pointer to iterate the elements
-when zero count reaches 2, repeatedly check the left pointer elements for 1,. This brings the left pointer to the next occurence of 0
-the right pointer - left pointer will give the window with max consecutive ones, when we add 1, this will fill the gap by flipping a zero to 1
*/
public static int findMaxConsecutiveOnes(int[] nums) {
int zeros = 0, max = 0;
for(int l =0, r =0; r<nums.length; r++){
if(nums[r]==0) zeros++;
if(zeros==2){
while(nums[l]==1){l++;}
l++;
zeros--;
}
max = Math.max(max, r-l+1);
System.out.println(l+"..."+r+"..."+max);
}
return max;
}
/**
Followup:
What if the input numbers come in one by one as an infinite stream? In other words, you can't store all numbers coming from the stream as it's too large to hold in memory. Could you solve it efficiently?
*/
public static int findMaxConsecutiveOnesInStream(int[] nums) {
int max = 0, k = 1; // flip at most k zero
Queue<Integer> zeroIndex = new LinkedList<>();
for (int l = 0, r = 0; r < nums.length; r++) {
if (nums[r] == 0)
zeroIndex.offer(r);
if (zeroIndex.size() > k)
l = zeroIndex.poll() + 1;
max = Math.max(max, r - l + 1);
}
return max;
}
public static void main(String[] args){
System.out.println(findMaxConsecutiveOnes(new int[]{1,0,1,1,0}));//4
System.out.println(findMaxConsecutiveOnes(new int[]{1,1,1,0,1,1,0}));//6
}
}
No comments:
Post a Comment