995. Minimum Number of K Consecutive Bit Flips Hard
1/**
2 * [995] Minimum Number of K Consecutive Bit Flips
3 *
4 * You are given a binary array nums and an integer k.
5 * 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.
6 * 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.
7 * A subarray is a contiguous part of an array.
8 *
9 * Example 1:
10 *
11 * Input: nums = [0,1,0], k = 1
12 * Output: 2
13 * Explanation: Flip nums[0], then flip nums[2].
14 *
15 * Example 2:
16 *
17 * Input: nums = [1,1,0], k = 2
18 * Output: -1
19 * Explanation: No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
20 *
21 * Example 3:
22 *
23 * Input: nums = [0,0,0,1,0,1,1,0], k = 3
24 * Output: 3
25 * Explanation:
26 * Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
27 * Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
28 * Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= nums.length <= 10^5
34 * 1 <= k <= nums.length
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/
40// discuss: https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn min_k_bit_flips(nums: Vec<i32>, k: i32) -> i32 {
46 0
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_995() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.