Problem · String

Get Minimum Operations

Learn this problem
MediumIBMFULLTIMEOA
See IBM hiring insights

Problem statement

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.

Function

getMinOperations(s: String, m: int, k: int) → int

Examples

Example 1

s = "000000"m = 3k = 2return = 1

We can perform an operation on the interval [3, 4] (1-based indexing) to get "001100", ensuring no segment of consecutive 0s has a length ≥ 3. Thus, the answer is 1.

Constraints

  • 1 ≤ n ≤ 2 * 10^5
  • 1 ≤ m, k ≤ n

More IBM problems

drafts saved locally
public int getMinOperations(String s, int m, int k) {
  // write your code here
}
s"000000"
m3
k2
expected1
checking account