Minimum Number Of K Consecutive Bit Flips Problem
Description
LeetCode Problem 995.
You are given a binary array nums and an integer k.
A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.
Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not possible, return -1.
A subarray is a contiguous part of an array.
Example 1:
Input: nums = [0,1,0], k = 1
Output: 2
Explanation: Flip nums[0], then flip nums[2].
Example 2:
Input: nums = [1,1,0], k = 2
Output: -1
Explanation: No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
Example 3:
Input: nums = [0,0,0,1,0,1,1,0], k = 3
Output: 3
Explanation:
Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
Constraints:
- 1 <= nums.length <= 10^5
- 1 <= k <= nums.length
Solution
Approach: Greedy Left to Right with a Sliding Flip Count
Look at the leftmost bit, nums[0]. Only one window covers it: the one starting at index 0. So if it is 0, that flip is forced; if it is 1, flipping there would only break it. After deciding index 0, the same holds for index 1, and so on: scanning left to right, every decision is forced. Also, flipping the same window twice cancels out, and the order of flips does not matter, so this forced sequence is the minimum.
Flipping the k bits for real each time would cost O(n · k). Instead, track how many flips cover the current position:
flipStartsAt[i]records whether we started a flip ati.activeFlipscounts the flips coveringi. When we reachi, the flip that started ati - k(if any) no longer covers it, so decreaseactiveFlips.- Each covering flip toggles the bit, so its current value is 0 exactly when
nums[i] + activeFlipsis even. In that case we must start a flip ati: ifi + k > nthe window would run past the end, so return-1; otherwise markflipStartsAt[i], and increaseactiveFlipsand the answer.
The detail people get wrong is the parity: the current bit depends on whether activeFlips is odd or even, not on whether any flip is active. Example 3, [0,0,0,1,0,1,1,0] with k = 3: flips start at indices 0, 4 and 5, so the answer is 3.
Complexity: O(n) time and O(n) space for flipStartsAt, where n is the length of nums.
class Solution:
def minKBitFlips(self, nums: List[int], k: int) -> int:
n = len(nums)
flip_starts_at = [False] * n
active_flips = 0 # number of flips that cover position i
total_flips = 0
for i in range(n):
if i >= k and flip_starts_at[i - k]:
active_flips -= 1 # the flip that started at i - k ended at i - 1
# each covering flip toggles the bit, so it is 0 now when nums[i] + active_flips is even
if (nums[i] + active_flips) % 2 == 0:
if i + k > n:
return -1 # a flip starting here would run past the end
flip_starts_at[i] = True
active_flips += 1
total_flips += 1
return total_flips
Now write it from memory
Close the write-up and solve it yourself against the tests.