|
| 1 | +# [Problem 3666: Minimum Operations to Equalize Binary String](https://leetcode.com/problems/minimum-operations-to-equalize-binary-string/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We need to turn a binary string s into all '1's by performing operations that flip exactly k distinct indices each time. Each operation is a k-sized subset of indices; the overall effect is the XOR (symmetric difference) of the chosen subsets over GF(2). The target flip-vector t has 1s where s has '0'. So the problem becomes: represent t as the XOR of m vectors each of Hamming weight exactly k, minimizing m. |
| 5 | + |
| 6 | +Immediate necessary conditions: |
| 7 | +- Parity: sum(t) (number of zeros) ≡ m*k (mod 2). |
| 8 | +- Trivial cases: if no zeros -> 0 operations. If k == n then each operation flips all bits; only two possible outcomes (no-op or global flip), so only z==0 or z==n can be achieved. |
| 9 | +- For k = 1: each operation flips one bit, so answer is number of zeros. |
| 10 | + |
| 11 | +But parity conditions are not sufficient — for example, one operation of k=3 produces exactly 3 ones (positions flipped), so you cannot obtain 1 flipped bit with a single k=3 operation even if parity allows it. So we need a more precise feasibility condition for a given m: whether we can assign for each index i the number c_i (0..m) of operations that include i so sum c_i = m*k and the number of indices with c_i odd equals z. That leads to integer constraints to check feasibility for given m. |
| 12 | + |
| 13 | +## Refining the problem, round 2 thoughts |
| 14 | +Model counts c_i ∈ [0, m] for all n positions. Let T = m*k, r = z (number of indices that must be odd). For indices with odd c_i write c_i = 2a_i + 1, for even indices c_i = 2b_i. Then |
| 15 | +T = r + 2 * S where S = sum a_i + sum b_i (so (T - r) must be even). |
| 16 | +Also a_i ≤ (m-1)//2 and b_i ≤ m//2 because c_i ≤ m. |
| 17 | +Hence feasibility for given m requires: |
| 18 | +- T ≥ r (sum picks at least #odd indices because each odd index needs ≥1 pick), |
| 19 | +- (T - r) % 2 == 0, |
| 20 | +- Let S = (T - r)//2, then S ≤ r * ((m-1)//2) + (n-r) * (m//2). |
| 21 | + |
| 22 | +So for minimal m we can iterate m from 1..n (0 handled earlier) and test these conditions. If none works return -1. |
| 23 | + |
| 24 | +Time complexity: O(n) iterations with O(1) checks each = O(n). n up to 1e5 fits. |
| 25 | + |
| 26 | +Edge cases handled explicitly: |
| 27 | +- z == 0 -> return 0 |
| 28 | +- k == n -> only z==0 -> 0 or z==n -> 1 else -1 |
| 29 | + |
| 30 | +This approach is simple, robust and matches the combinatorial feasibility constraints. |
| 31 | + |
| 32 | +## Attempted solution(s) |
| 33 | +```python |
| 34 | +class Solution: |
| 35 | + def minOperations(self, s: str, k: int) -> int: |
| 36 | + n = len(s) |
| 37 | + z = s.count('0') |
| 38 | + if z == 0: |
| 39 | + return 0 |
| 40 | + # Special case: k == n -> each operation flips all bits. |
| 41 | + if k == n: |
| 42 | + # Only possible to make all ones if s already all ones (z==0) or s all zeros (one flip) |
| 43 | + return 1 if z == n else -1 |
| 44 | + |
| 45 | + # Try m from 1..n (0 already handled). For each m check feasibility. |
| 46 | + for m in range(1, n + 1): |
| 47 | + T = m * k |
| 48 | + # must have at least one pick per odd-index (each odd c_i >= 1) |
| 49 | + if T < z: |
| 50 | + continue |
| 51 | + # parity constraint: T and z must have same parity |
| 52 | + if (T - z) & 1: |
| 53 | + continue |
| 54 | + S = (T - z) // 2 |
| 55 | + # capacity is how many pairs (2 picks) we can allocate across indices given upper bounds |
| 56 | + cap = z * ((m - 1) // 2) + (n - z) * (m // 2) |
| 57 | + if S <= cap: |
| 58 | + return m |
| 59 | + return -1 |
| 60 | +``` |
| 61 | +- Notes about the solution approach: |
| 62 | + - We convert the problem into a feasibility check for counts c_i ∈ [0, m] that sum to T = m*k and have exactly z indices with odd c_i. |
| 63 | + - The algebraic reduction leads to checking parity, minimum total picks, and whether the remainder S = (T - z)/2 can be distributed within per-index pair capacities ((m-1)//2 for odd indices, m//2 for even indices). |
| 64 | + - We handle k == n explicitly because all operations are identical in that case. |
| 65 | + - Complexity: O(n) time and O(1) extra space (beyond input). |
0 commit comments