clueless coding // TODO: be smarter

LeetCode Max Consecutive Ones II: CPP Solution



TL;DR Code Solution


int findMaxConsecutiveOnes(vector<int>& nums) {

    bool hasZero = false;

    int left = 0;
    int right = 0;

    int result = 0;

    while (right < nums.size()) {

        if (nums[right] == 0) {
            if (hasZero) {
                while (nums[left] != 0) {
                    left++;
                }
                left++;
            }
            else {
                hasZero = true;
            }
        }
        right++;
        result = max(result, right - left);
    }

    return result;


}