995. Minimum Number of K Consecutive Bit Flips
Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 62% Topics: Array, Bit Manipulation, Queue, Sliding Window, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def minKBitFlips(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
result, curr = 0, 0
for i in xrange(len(A)):
if i >= K:
curr -= A[i-K]//2
if curr & 1 ^ A[i] == 0:
if i+K > len(A):
return -1
A[i] += 2
curr, result = curr+1, result+1
return result
Solution from kamyu104/LeetCode-Solutions · MIT