Python 3 simple and efficient O(N) solution using queue


#1
from collections import deque 


class Solution:
    def solve(self, A, B):
        ans = 0
        q = deque([-1])
        for i, a in enumerate(A):
            if a == 0:
                if len(q) == B + 1:
                    ans = max(ans, i - 1 - q.popleft())
                q.append(i)
    
        ans = max(ans, len(A) - 1 - q.popleft())
        return ans