Count Prioritized Binary-Run Operations
Problem statement
bits consists of zero or more 1s followed by zero or more 0s, and k >= 2. Repeatedly apply the first available rule:
- If at least k zeros exist, remove the last k zeros and prepend one 1.
- Otherwise, if a 1 exists, replace the last 1 with 0.
Stop when neither rule applies and return the number of operations.
Function
countBinaryOperations(bits: String, k: int) → longExamples
Example 1
bits = "10"k = 2return = 3The states by counts are (1,1), (0,2), (1,0), and (0,1).
Example 2
bits = "00"k = 2return = 2Compress two zeros to one 1, then convert that 1 to one zero.
Constraints
1 <= bits.length <= 1000002 <= k <= 100000- The operation count fits in signed 64-bit.