Class Solution
-
- All Implemented Interfaces:
public final class Solution995 - Minimum Number of K Consecutive Bit Flips\.
Hard
You are given a binary array
numsand an integerk.A k-bit flip is choosing a subarray of length
kfromnumsand simultaneously changing every0in the subarray to1, and every1in the subarray to0.Return the minimum number of k-bit flips required so that there is no
0in 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 <= k <= nums.length
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminKBitFlips(IntArray nums, Integer k)-
-
Method Detail
-
minKBitFlips
final Integer minKBitFlips(IntArray nums, Integer k)
-
-
-
-