Other way to think about it


  • 1
    N

    flip to 1 to 0, and 0 to 1, this problem changed to other problem:
    find a continuous sub array, its sum is less than 2,
    ...
    int findMaxConsecutiveOnes(int* nums, int numsSize) {

    int t = 0, max = 0, sum=0, zeroafter1=0;
    for(int i=0; i<numsSize; ++i) {
        nums[i] ^= 1;
        sum += nums[i];
        if (sum ==1) {
            if (nums[i]==0) ++zeroafter1;
        }  // current is 1
    
        else if (sum ==2) { // curent is 1
            sum = 1;
            if (t>max) {
                max = t;
            }
            t = zeroafter1;
            zeroafter1 = 0;
        }
        else {} // current is 0
        ++t;
    }
    if (t>max) max = t; 
    return max;
    

    }
    ...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.