Leetcode 487 Solution

This article provides solution to leetcode question 487 (max-consecutive-ones-ii)

https://leetcode.com/problems/max-consecutive-ones-ii

Solution

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        deque<int> q;
        q.push_back(-1);

        int max_len = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] == 0)
                q.push_back(i);

            if (q.size() > 2)
                q.pop_front();

            max_len = max(max_len, i - q.front());
        }

        return max_len;
    }
};