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.
For example:
Input: nums = [0,1,0], k = 1 Output: 2 Explanation: Flip nums[0], then flip nums[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].
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]
def min_k_bit_flips(nums, k):
n = len(nums)
flips = 0
flipped = [0] * n # flipped[i] indicates if the subarray ending at i has been flipped
curr_flips = 0
for i in range(n):
if i >= k:
curr_flips -= flipped[i - k]
if (nums[i] + curr_flips) % 2 == 0:
if i + k > n:
return -1
flips += 1
curr_flips += 1
flipped[i] = 1
return flips
# Example 1
nums1 = [0, 1, 0]
k1 = 1
print(f"Example 1: nums = {nums1}, k = {k1}, Output = {min_k_bit_flips(nums1, k1)}")
# Example 2
nums2 = [1, 1, 0]
k2 = 2
print(f"Example 2: nums = {nums2}, k = {k2}, Output = {min_k_bit_flips(nums2, k2)}")
# Example 3
nums3 = [0, 0, 0, 1, 0, 1, 1, 0]
k3 = 3
print(f"Example 3: nums = {nums3}, k = {k3}, Output = {min_k_bit_flips(nums3, k3)}")
# Edge Case 1: k is larger than nums length
nums4 = [0,1]
k4 = 3
print(f"Edge Case 1: nums = {nums4}, k = {k4}, Output = {min_k_bit_flips(nums4, k4)}")
# Edge Case 2: nums is already all 1s
nums5 = [1,1,1]
k5 = 2
print(f"Edge Case 2: nums = {nums5}, k = {k5}, Output = {min_k_bit_flips(nums5, k5)}")
The function min_k_bit_flips(nums, k)
calculates the minimum number of k-bit flips required to make all elements in the nums
array equal to 1. It returns -1 if it's impossible.
Initialization:
flips
: Counts the number of k-bit flips.flipped
: A list to track if a flip has been performed ending at index i
. This helps to keep track of the current number of active flips.curr_flips
: Keeps track of the number of active flips affecting the current element.Iteration:
nums
array.i
is greater than or equal to k
, it means a previous flip's effect might no longer be active. Remove flipped[i - k]
from curr_flips
.nums[i]
plus the current number of active flips curr_flips
results in an even number (which means 0). If so, a flip is needed.flips
and curr_flips
, and mark flipped[i]
as 1 to indicate that a flip has been performed ending at the current index.Return:
The time complexity of the solution is O(n), where n is the length of the nums
array. This is because the code iterates through the array once.
The space complexity of the solution is O(n), where n is the length of the nums
array. This is due to the flipped
array that keeps track of the flips performed. Note that curr_flips
is only an integer so does not impact the overall space complexity.
k is larger than nums length:
k
is larger than the length of nums
, it's impossible to perform a k-bit flip on the entire array. It should return -1 if a zero remains in the array after one attempted flip.nums is already all 1s:
nums
is already all 1s, no flips are needed, and the function should return 0.Array cannot be converted to all 1s: