Trains the technique from
LeetCode 926Flip String to Monotone IncreasingThis 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 row of switches reads row, each character '0' for down and '1' for up. One throw changes a single switch, either way.
The row is settled when no switch that is down comes after a switch that is up: all the downs first, then all the ups, and either group may be empty.
Return the fewest throws that settle the row.
Example 1
Three ups then three downs. Throwing all three ups down settles the row, and so does throwing all three downs up, either way three throws. Nothing shorter works, since every up sits before every down.
Example 2
Every switch is down, which is already settled.
Example 3
One up followed by one down. A single throw on either switch settles it.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def min_flips_mono_incr(row: str) -> int:public int minFlipsMonoIncr(String row)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.