Class Solution
java.lang.Object
g0901_1000.s0995_minimum_number_of_k_consecutive_bit_flips.Solution
995 - Minimum Number of K Consecutive Bit Flips.<p>Hard</p>
<p>You are given a binary array <code>nums</code> and an integer <code>k</code>.</p>
<p>A <strong>k-bit flip</strong> is choosing a <strong>subarray</strong> of length <code>k</code> from <code>nums</code> and simultaneously changing every <code>0</code> in the subarray to <code>1</code>, and every <code>1</code> in the subarray to <code>0</code>.</p>
<p>Return <em>the minimum number of <strong>k-bit flips</strong> required so that there is no</em> <code>0</code> <em>in the array</em>. If it is not possible, return <code>-1</code>.</p>
<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [0,1,0], k = 1</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> Flip nums[0], then flip nums[2].</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [1,1,0], k = 2</p>
<p><strong>Output:</strong> -1</p>
<p><strong>Explanation:</strong> No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [0,0,0,1,0,1,1,0], k = 3</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]</p>
<p>Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]</p>
<p>Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minKBitFlips
public int minKBitFlips(int[] nums, int k)
-