Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    995 - Minimum Number of K Consecutive Bit Flips.

    Hard

    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 nums0, then flip nums2.

    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 nums0,nums1,nums2: nums becomes 1,1,1,1,0,1,1,0

    Flip nums4,nums5,nums6: nums becomes 1,1,1,1,1,0,0,0

    Flip nums5,nums6,nums7: nums becomes 1,1,1,1,1,1,1,1

    Constraints:

    • <code>1 <= nums.length <= 10<sup>5</sup></code>

    • 1 &lt;= k &lt;= nums.length

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer minKBitFlips(IntArray nums, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait