Related:
LeetCode 485 - Max Consecutive Ones
LeetCode 487 - Max Consecutive Ones II
http://bookshadow.com/weblog/2017/01/15/leetcode-max-consecutive-ones-ii/
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?
https://discuss.leetcode.com/topic/75445/java-clean-solution-easily-extensible-to-flipping-k-zero-and-follow-up-handled
X.
https://discuss.leetcode.com/topic/75457/sliding-window-2-pointers-o-n-time-1-pass-o-1-space
https://discuss.leetcode.com/topic/75426/concise-o-n-solution-slight-modification-of-two-pointers-and-1-liner-solution-for-fun/2
https://discuss.leetcode.com/topic/75439/java-concise-o-n-time-o-1-space
Use variable index to record the position of zero, and initiate the index to -1. Once we find a zero, refresh the count and store the new index.
X. https://discuss.leetcode.com/topic/75435/java-dp-o-n-solution
LeetCode 485 - Max Consecutive Ones
LeetCode 487 - Max Consecutive Ones II
http://bookshadow.com/weblog/2017/01/15/leetcode-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:
Note:
- The input array will only contain
0
and1
. - 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?
https://discuss.leetcode.com/topic/75445/java-clean-solution-easily-extensible-to-flipping-k-zero-and-follow-up-handled
X.
https://discuss.leetcode.com/topic/75457/sliding-window-2-pointers-o-n-time-1-pass-o-1-space
public int findMaxConsecutiveOnes(int[] nums) {
int j=0;
int len=0;
int zero=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
zero++;
}
while(zero>1){
if(nums[j]==0){
zero--;
}
j++;
}
len=Math.max(i-j+1,len);
}
return len;
}
https://discuss.leetcode.com/topic/75426/concise-o-n-solution-slight-modification-of-two-pointers-and-1-liner-solution-for-fun/2
Based on the above solution, for "Max Consecutive Ones II", we simply keep track of 2 low pointers:
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
nums.append(0)
lo1, lo2 = 0, 0
ret = 0
for hi,n in enumerate(nums):
if n==0:
ret = max(ret, hi-lo1)
lo1, lo2 = lo2, hi+1
return ret
https://discuss.leetcode.com/topic/75439/java-concise-o-n-time-o-1-space
Use variable index to record the position of zero, and initiate the index to -1. Once we find a zero, refresh the count and store the new index.
int max = 0, count = 0, index = -1;
for(int i = 0;i< nums.length; i++){
if(nums[i] == 1){
count++;
}else{
count = i - index;
index = i;
}
max = Math.max(max,count);
}
return max;
}
public int findMaxConsecutiveOnes(int[] nums) {
int maxConsecutive = 0, zeroLeft = 0, zeroRight = 0;
for (int i=0;i<nums.length;i++) {
zeroRight++;
if (nums[i] == 0) {
zeroLeft = zeroRight;
zeroRight = 0;
}
maxConsecutive = Math.max(maxConsecutive, zeroLeft+zeroRight);
}
return maxConsecutive;
}
线性遍历+计数器
统计恰好相隔1个'0'的两个连续1子数组的最大长度之和
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == sum(nums): return sum(nums)
a = b = c = 0
for n in nums:
if n == 1:
c += 1
else:
b, c = c, 0
a = max(a, b + c + 1)
return aX. https://discuss.leetcode.com/topic/75435/java-dp-o-n-solution
The idea is to use an extra array
dp
to store number of 1
to its left and right. Then the answer is the MAX
of dp[i] + 1
of all nums[i] == 0
. public int findMaxConsecutiveOnes(int[] nums) {
int result = 0, n = nums.length, count = 0;
int[] dp = new int[n];
for (int i = 0; i < n; i++) {
dp[i] = count;
if (nums[i] == 1) {
count++;
result = Math.max(count, result);
}
else count = 0;
}
count = 0;
for (int i = n - 1; i >= 0; i--) {
dp[i] += count;
if (nums[i] == 1) count++;
else count = 0;
}
for (int i = 0; i < n; i++) {
if (nums[i] == 0) {
result = Math.max(result, dp[i] + 1);
}
}
return result;
}