# Leetcode 995：K 连续位的最小翻转次数（超详细的解法！！！）

输入：A = [0,1,0], K = 1



输入：A = [1,1,0], K = 2



输入：A = [0,0,0,1,0,1,1,0], K = 3



1. 1 <= A.length <= 30000
2. 1 <= K <= A.length

class Solution:
def minKBitFlips(self, A: 'List[int]', K: 'int') -> 'int':
res, n = 0, len(A)
for i, a in enumerate(A):
if a == 0:
if n - i < K:
return -1
for j in range(i, i + K):
A[j] ^= 1
res += 1
return res


class Solution:
def minKBitFlips(self, A: 'List[int]', K: 'int') -> 'int':
q, res, n = list(), 0, len(A)
for i, a in enumerate(A):
if q and q[0] + K == i:
q.pop(0)

if (len(q) + a) & 1 == 0:
if i > n - K:
return -1
q.append(i)
res += 1

return res


class Solution:
def minKBitFlips(self, A: 'List[int]', K: 'int') -> 'int':
cur = res = 0
for i in range(len(A)):
if i >= K:
cur -= A[i - K] // 2
if (cur + A[i]) & 1 == 0:
if i + K > len(A):
return -1
A[i] += 2
cur, res = cur + 1, res + 1
return res


reference:

https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/discuss/238609/JavaC%2B%2BPython-One-Pass-and-O(1)-Space