AMAZON Coding Question β Solved
Implement a function that returns the minimum number of operations needed to ensure the string s (of length n) contains no segments of exactly m consecutive '0's.
In one operation, you can do the following:
- Select a contiguous segment of length k and make every bit in this segment '1'.
The function getMinOperations will take three inputs:
- string s: a string representing s.
- int m: an integer representing m.
- int k: an integer representing k.
The function should return an integer representing the minimum number of operations needed.
Example:
s = "000000"
m = 3
Interval [3, 4] (1-based indexing) to get "001100", ensuring no segment of consecutive 0's has a length β₯ 3. Thus, the answer is 1.
Constraints:
- 1 β€ n β€ 2 * 10^5
- 1 β€ m, k β€ n
- It is guaranteed that each character in s is '0' or '1'.
FUNCTION
s = "10101"
m = 1
Input Format for Custom Testing
Sample Case 0
Sample Input 0:
STDIN
10101
1
1
Sample Output 0:
2
Explanation:
- In one operation, we can flip only one bit.