All problems
0915HardStringBinary Search

Shortest Longest Run After Flips

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3399Smallest Substring With Identical Characters II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A tape reads s, a string of '0' and '1'. Up to numOps characters may be flipped to the other one, chosen freely and independently.

A run is a stretch of equal neighbouring characters. Make the tape's longest run as short as you can, and return that length.

Examples

Example 1

Input
s = "1100011100", numOps = 4
Output
1

Four flips are enough to leave no run longer than one, since the tape needs four characters changed to alternate throughout.

Example 2

Input
s = "0000000000", numOps = 1
Output
5

One flip splits the run of ten into pieces, and the best it can do is leave a longest run of five.

Example 3

Input
s = "0000000000", numOps = 0
Output
10

With no flips allowed the tape keeps its single run of ten.

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is either '0' or '1'
  • 0 <= numOps <= s.length

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_length(s: str, numOps: int) -> int:
Java
public int minLength(String s, int numOps)
September 7
Apply