Ross' solution is correct in that the method does find the minimal number of flip.
The idea is that we can visit each bit from the left to the right and flip the next $k$ bits. If one really code it up in the sense of visiting a bit and flipping the next $k$ bits directly, the complexity of the algorithm is $O(nk)$.
We can actually solve the problem in $O(n)$ times and $O(n)$ memory. The idea is to control the number of time we flip the bits at each position and use an additional variable instead.
An analogy is to think of the bits as a series of rooms with lights. A forgetful general visited the first room, he tells one of his soldier to push the switch for the consecutive $k$ rooms. But rather than running to the next $k$ rooms and push the switch immediately, he just keep a record on when should he stop pushing the switch accoding to this current instruction.
Just before the general enter the next room, the soldier implement the existing instruction for the current room. The main difference is rather than having to press a switch everytime an instruction is given, two instruction might cancels out each other for a particular room.
For example, if we have a very long string, $k=50$. Each time an instructionis given, the soldier doesn't run for the next $50$ rooms immediately, instead he will just take note that the instruction expired after the next $50$ rooms. Hence if a new instruction is given on the very next day and no further instruction is given after that. He doesn't have to press any switch for quite some time, he just have to remember if the net instruction if still active. On the days that the instruction expired, he has to change the instruction again.
def minKBitFlips(A, k):
n = len(A)
future_switch_order = [0 for i in range(n)]
switch = 0
ans = 0
for i in range(n):
switch ^= future_switch_order[i]
if A[i]^switch == 0:
ans += 1
switch ^= 1
if i+k < n:
future_switch_order[i+k] = 1
elif i+k > n: