You are given a binary string s, and an integer k.
In one operation, you must choose exactlykdifferent indices and flip each '0' to '1' and each '1' to '0'.
Return the minimum number of operations required to make all characters in the string equal to '1'. If it is not possible, return -1.
Β
Example 1:
Input:s = "110", k = 1
Output:1
Explanation:
There is one '0' in s.
Since k = 1, we can flip it directly in one operation.
Example 2:
Input:s = "0101", k = 3
Output:2
Explanation:
One optimal set of operations choosing k = 3 indices in each operation is:
Operation 1: Flip indices [0, 1, 3]. s changes from "0101" to "1000".
Operation 2: Flip indices [1, 2, 3]. s changes from "1000" to "1111".
Thus, the minimum number of operations is 2.
Example 3:
Input:s = "101", k = 2
Output:-1
Explanation:
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.
Β
Constraints:
1 <= s.length <= 10βββββββ5
s[i] is either '0' or '1'.
1 <= k <= s.length
Solutions
Solution 1: BFS + Ordered Set
We denote the length of string \(s\) as \(n\), and the current number of '0's in the string as \(\textit{cur}\). In each operation, we select \(k\) indices to flip, where \(x\) indices flip from '0' to '1', and \(k-x\) indices flip from '1' to '0'. Then the number of '0's in the string after flipping is \(\textit{cur} + k - 2x\).
The value of \(x\) needs to satisfy the following conditions:
At most \(\min(\textit{cur}, k)\) '0's can be taken, because we cannot flip more than \(\textit{cur}\) '0's, so \(0 \leq x \leq \min(\textit{cur}, k)\).
At most \(n - \textit{cur}\) '1's can be taken, because we cannot flip more than \(n - \textit{cur}\) '1's, so \(k - x \leq n - \textit{cur}\), i.e., \(x \geq k - n + \textit{cur}\).
Therefore, the range of \(x\) is \([\max(k - n + \textit{cur}, 0), \min(\textit{cur}, k)]\), and the range of the number of '0's in the string after flipping is \([\textit{cur} + k - 2 \cdot \min(\textit{cur}, k), \textit{cur} + k - 2 \cdot \max(k - n + \textit{cur}, 0)]\).
We notice that the parity of the number of '0's in the string after flipping is the same as the parity of the number of '0's in the string before flipping. Therefore, we can use two ordered sets to store states where the number of '0's is even and odd, respectively.
We use BFS to search the state transition graph, where the initial state is the number of '0's in the string, and the target state is 0. Each time we dequeue a state \(\textit{cur}\), we calculate the range \([l, r]\) of the number of '0's in the string after flipping, find all states in the range \([l, r]\) in the ordered set, add them to the queue, and remove them from the ordered set.
If we visit state 0 during the BFS process, we return the current number of operations; if state 0 is not visited after BFS ends, we return -1.
The time complexity is \(O(n \log n)\), and the space complexity is \(O(n)\), where \(O(n)\) is the number of states that may be visited during the BFS process, and \(O(\log n)\) is the time complexity of inserting and deleting elements in the ordered set.