3666. Minimum Operations to Equalize Binary String Hard

@problem@discussion
#Math#String#Breadth-First Search#Union-Find#Ordered Set



1/**
2 * [3666] Minimum Operations to Equalize Binary String
3 *
4 * You are given a binary string s, and an integer k.
5 * In one operation, you must choose exactly k different indices and flip each '0' to '1' and each '1' to '0'.
6 * Return the minimum number of operations required to make all characters in the string equal to '1'. If it is not possible, return -1.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "110", k = 1</span>
11 * Output: <span class="example-io">1</span>
12 * Explanation:
13 * 
14 * 	There is one '0' in s.
15 * 	Since k = 1, we can flip it directly in one operation.
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">s = "0101", k = 3</span>
20 * Output: <span class="example-io">2</span>
21 * Explanation:
22 * One optimal set of operations choosing k = 3 indices in each operation is:
23 * 
24 * 	Operation 1: Flip indices [0, 1, 3]. s changes from "0101" to "1000".
25 * 	Operation 2: Flip indices [1, 2, 3]. s changes from "1000" to "1111".
26 * 
27 * Thus, the minimum number of operations is 2.
28 * </div>
29 * <strong class="example">Example 3:
30 * <div class="example-block">
31 * Input: <span class="example-io">s = "101", k = 2</span>
32 * Output: <span class="example-io">-1</span>
33 * Explanation:
34 * Since k = 2 and s has only one '0', it is impossible to flip exactly k indices to make all '1'. Hence, the answer is -1.
35 * </div>
36 *  
37 * Constraints:
38 * 
39 * 	1 <= s.length <= 10^​​​​​​​5
40 * 	s[i] is either '0' or '1'.
41 * 	1 <= k <= s.length
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-operations-to-equalize-binary-string/
47// discuss: https://leetcode.com/problems/minimum-operations-to-equalize-binary-string/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn min_operations(s: String, k: i32) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_3666() {
65    }
66}
67

Back
© 2026 bowen.ge All Rights Reserved.