LintCode 883: Max Consecutive Ones II

883. 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

Example 1:

    Input:  nums = [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.

 

Example 2:

    Input: nums = [1,0,1,0,1]

    Output:  3

    

    Explanation:

    Flip each zero will get the the maximum number of consecutive 1s.

    After flipping, the maximum number of consecutive 1s is 3.

    

Notice

The input array will only contain 0 and 1.

The length of input array is a positive integer and will not exceed 10,000.

解法1: DP

class Solution {
public:
    /**
     * @param nums: a list of integer
     * @return: return a integer, denote  the maximum number of consecutive 1s
     */
    int findMaxConsecutiveOnes(vector<int> &A) {
        int n = A.size();
        
        //dp_0[i]: the maximum number of consecutive, end with A[i], never flipped so far
        //dp_1[i]: the maximum number of consecutive, end with A[i], flipped at most once
        vector<int> dp_0(n, 0), dp_1(n, 0);
        if (A[0] == 0) {
            dp_0[0] = 0;
            dp_1[0] = 1;
        } else {
            dp_0[0] = 1;
            dp_1[0] = 1;
        }
        int max_len = 1;
        for (int i = 1; i < n; ++i) {
            if (A[i] == 0) {
                dp_0[i] = 0;
                dp_1[i] = dp_0[i - 1] + 1;
            } else {
                dp_0[i] = dp_0[i - 1] + 1;
                dp_1[i] = dp_1[i - 1] + 1;
            }
            max_len = max(max_len, dp_1[i]);
        }
        
       return max_len;
    }
};

空间优化版如下:
 

class Solution {
public:
    /**
     * @param nums: a list of integer
     * @return: return a integer, denote  the maximum number of consecutive 1s
     */
    int findMaxConsecutiveOnes(vector<int> &A) {
        int n = A.size();
        
        //dp_0: the maximum number of consecutive, end with A[i], never flipped so far
        //dp_1: the maximum number of consecutive, end with A[i], flipped at most once
        int dp_0 = 0, dp_1 = 0;
        if (A[0] == 0) {
            dp_0 = 0;
            dp_1 = 1;
        } else {
            dp_0 = 1;
            dp_1 = 1;
        }
        int max_len = 1;
        for (int i = 1; i < n; ++i) {
            if (A[i] == 0) {
                dp_1 = dp_0 + 1;   //remember to bring dp_1 in the front of dp_0, otherwise it will be cleared.
                dp_0 = 0;
            } else {
                dp_0++;
                dp_1++;
            }
            max_len = max(max_len, dp_1);
        }
        
       return max_len;
    }
};

也可以用滚动数组来优化空间,代码如下:
 

class Solution {
public:
    /**
     * @param nums: a list of integer
     * @return: return a integer, denote  the maximum number of consecutive 1s
     */
    int findMaxConsecutiveOnes(vector<int> &A) {
        int n = A.size();
        
        //dp_0[i]: the maximum number of consecutive, end with A[i], never flipped so far
        //dp_1[i]: the maximum number of consecutive, end with A[i], flipped at most once
        vector<int> dp_0(2, 0), dp_1(2, 0);
        if (A[0] == 0) {
            dp_0[0] = 0;
            dp_1[0] = 1;
        } else {
            dp_0[0] = 1;
            dp_1[0] = 1;
        }
        int max_len = 1;
        for (int i = 1; i < n; ++i) {
            if (A[i] == 0) {
                dp_0[i % 2] = 0;
                dp_1[i % 2] = dp_0[(i - 1) % 2] + 1;
            } else {
                dp_0[i % 2] = dp_0[(i - 1) % 2] + 1;
                dp_1[i % 2] = dp_1[(i - 1) % 2] + 1;
            }
            max_len = max(max_len, dp_1[i % 2]);
        }
        
       return max_len;
    }
};


解法2:
 

class Solution {
public:
    /**
     * @param nums: a list of integer
     * @return: return a integer, denote  the maximum number of consecutive 1s
     */
    int findMaxConsecutiveOnes(vector<int> &nums) {
        int len = nums.size();
        int prev_count = 0, curr_count = 0, max_count = 0;
        bool connect_ok = false;
        
        for (int i = 0; i < len; ++i) {
            if (nums[i] == 1) {
                curr_count++;
            } else {
                if (connect_ok) {
                    max_count = max(max_count, prev_count + curr_count + 1);
                    connect_ok = false;
                }
                prev_count = curr_count;
                curr_count = false;

                if (i < len - 1 && i > 0 && nums[i - 1] == 1 && nums[i + 1] == 1) {
                    connect_ok = true;
                }
            }
        }
        
        // for the case that reaches the end
        if (connect_ok) {
            max_count = max(max_count, prev_count + curr_count + 1);
        }
        
        return max_count;
    }
};

解法3:类似解法2,更简洁。
 

class Solution {
public:
    /**
     * @param nums: a list of integer
     * @return: return a integer, denote  the maximum number of consecutive 1s
     */
    int findMaxConsecutiveOnes(vector<int> &A) {
        int n = A.size();
        int ans = 0, pos = 0, prev_count = 0, curr_count = 0;
        for (int i = 0; i < n; i++) {
            if( A[i] == 1) {
                curr_count++;
            }else{
                ans = max(ans, curr_count + prev_count + 1);
                prev_count = curr_count;
                curr_count = 0;
            }
        }
        
        return ans; 
    }
};

解法4:这个解法是在九章网上看到的,感觉很牛。
原文描述如下:

非常简洁的双指针解法,可以解决 flip K 个0的 follow up.
思路: 右指针一路往右边扫,遇到0就减少K,当K的值变成了负数,说明目前K的指标已经用完了,此时的操作相当于把整个sliding window 向右 平移一格,如果左边开头的是1,那么“拆东墙补西墙”,把最左的1移到当前最右。如果左边是0, 那么恭喜你,您又多获得了一个允许flip的指标。
一遍扫完,前后指针之间的距离就是最大长度。

class Solution {
public:
    /**
     * @param nums: a list of integer
     * @return: return a integer, denote  the maximum number of consecutive 1s
     */
    int findMaxConsecutiveOnes(vector<int> &A) {
        int K = 1;
        int n = A.size();
        int i = 0, j = 0;
        for (j = 0; j < n; ++j) {
            if (A[j] == 0) --K;
            //if (K < 0 && A[i++] == 0) ++K;
            if (K < 0) {
                if (A[i] == 0) ++K;
                i++;
            }
          //  cout<<"j="<<j<<" i="<<i<<" K="<<K<<endl;
        }
        return j - i;
    }
};

为了理解这个算法,看看下面这个case:
Input

[1,1,0,1,1,1,0,0,1,1,1,1,0,1,1,1]

Your stdout

j=0 i=0 K=1
j=1 i=0 K=1
j=2 i=0 K=0
j=3 i=0 K=0
j=4 i=0 K=0
j=5 i=0 K=0
j=6 i=1 K=-1
j=7 i=2 K=-2
j=8 i=3 K=-1
j=9 i=4 K=-1
j=10 i=5 K=-1
j=11 i=6 K=-1
j=12 i=7 K=-1
j=13 i=8 K=0
j=14 i=8 K=0
j=15 i=8 K=0

结果为8。下图显示了i和j的变化。
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值