995. Minimum Number of K Consecutive Bit Flips

Dare2Solve

Dare2Solve

995. Minimum Number of K Consecutive Bit Flips
SAMSUNG 49-Inch Odyssey G9
SAMSUNG 49-Inch Odyssey G9
Because earth is not flat

Intuition

To solve the problem of flipping k-bit subarrays in a binary array nums so that there are no 0s left, we need to determine the minimum number of flips required. If it's not possible to achieve this, we return -1.

The key intuition is to track the flip operations using a sliding window technique. By keeping track of the current state of flips, we can efficiently decide whether to flip the subarray starting at the current index or not.

Approach

  1. Initialize Variables:

    • flag to keep track of the cumulative flip state.
    • res to count the number of flips performed.
    • n to store the length of the input array nums.
  2. Iterate Through the Array:

    • For each element nums[i]:
      • Calculate the current state cur by XORing nums[i] with flag.
      • If cur is 0, it means the current bit is 0 and needs to be flipped.
        • If flipping a subarray starting at i would exceed the bounds of the array (i + k > n), return -1.
        • Otherwise, increment the flip counter res and toggle the flag to indicate a new flip operation.
      • After potentially flipping, set nums[i] to the flipped value (1 - cur).
  3. Adjust Flip State:

    • After processing the current element, update the flag based on the element that falls out of the flip window (i.e., i + 1 - k).
  4. Return the Result:

    • Return the total number of flips res.

Complexity

Code Explanation

Function Signature

var minKBitFlips = function (nums, k) {

Variables

var flag = 0;
var res = 0;
var n = nums.length;

Main Loop

for (var i = 0; i < n; i++) {

The loop iterates through each element of the array nums.

Determine Current Bit State

var cur = nums[i] ^ flag;

Check If Flip is Needed

if (cur === 0) {
    if (i + k > n) return -1;
    flag ^= 1;
    res++;
}

Mark the Flip

nums[i] = 1 - cur;

Maintain Flip State

if (i + 1 - k >= 0) flag ^= nums[i + 1 - k];

Return Result

return res;

This function efficiently determines the minimum number of k-bit flips required to ensure there are no 0s in the array nums, or returns -1 if it's not possible.

Code

C++

class Solution {
public:
    int minKBitFlips(vector<int>& nums, int k) {
        int flag = 0;
        int res = 0;
        int n = nums.size();

        for (int i = 0; i < n; i++) {
            int cur = nums[i] ^ flag;
            if (cur == 0) {
                if (i + k > n) return -1;
                flag ^= 1;
                res++;
            }
            nums[i] = 1 - cur;
            if (i + 1 - k >= 0) flag ^= nums[i + 1 - k];
        }
        return res;
    }
};

Python

class Solution:
    def minKBitFlips(self, nums: List[int], k: int) -> int:
        flag = 0
        res = 0
        n = len(nums)

        for i in range(n):
            cur = nums[i] ^ flag
            if cur == 0:
                if i + k > n:
                    return -1
                flag ^= 1
                res += 1
            nums[i] = 1 - cur
            if i + 1 - k >= 0:
                flag ^= nums[i + 1 - k]
        
        return res

Java

class Solution {
    public int minKBitFlips(int[] nums, int k) {
        int flag = 0;
        int res = 0;
        int n = nums.length;

        for (int i = 0; i < n; i++) {
            int cur = nums[i] ^ flag;
            if (cur == 0) {
                if (i + k > n) return -1;
                flag ^= 1;
                res++;
            }
            nums[i] = 1 - cur;
            if (i + 1 - k >= 0) flag ^= nums[i + 1 - k];
        }
        return res;
    }
}

JavaScript

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var minKBitFlips = function (nums, k) {
    var flag = 0;
    var res = 0;
    var n = nums.length;

    for (var i = 0; i < n; i++) {
        var cur = nums[i] ^ flag;
        if (cur === 0) {
            if (i + k > n) return -1;
            flag ^= 1;
            res++;
        }
        nums[i] = 1 - cur;
        if (i + 1 - k >= 0) flag ^= nums[i + 1 - k];
    }
    return res;
};