Problem summary: A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none). You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0. Return the minimum number of flips to make s monotone increasing.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Dynamic Programming
Example 1
"00110"
Example 2
"010110"
Example 3
"00011000"
Related Problems
Minimum Cost to Make All Characters Equal (minimum-cost-to-make-all-characters-equal)
Step 02
Core Insight
What unlocks the optimal approach
No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #926: Flip String to Monotone Increasing
class Solution {
public int minFlipsMonoIncr(String s) {
int n = s.length();
int tot = 0;
for (int i = 0; i < n; ++i) {
if (s.charAt(i) == '0') {
++tot;
}
}
int ans = tot, cur = 0;
for (int i = 1; i <= n; ++i) {
if (s.charAt(i - 1) == '0') {
++cur;
}
ans = Math.min(ans, i - cur + tot - cur);
}
return ans;
}
}
// Accepted solution for LeetCode #926: Flip String to Monotone Increasing
func minFlipsMonoIncr(s string) int {
tot := strings.Count(s, "0")
ans, cur := tot, 0
for i, c := range s {
if c == '0' {
cur++
}
ans = min(ans, i+1-cur+tot-cur)
}
return ans
}
# Accepted solution for LeetCode #926: Flip String to Monotone Increasing
class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
tot = s.count("0")
ans, cur = tot, 0
for i, c in enumerate(s, 1):
cur += int(c == "0")
ans = min(ans, i - cur + tot - cur)
return ans
// Accepted solution for LeetCode #926: Flip String to Monotone Increasing
impl Solution {
pub fn min_flips_mono_incr(s: String) -> i32 {
let (mut res, mut count_one) = (0, 0);
for ch in s.chars() {
if ch == '1' {
count_one += 1;
} else {
res = i32::min(res + 1, count_one);
}
}
res
}
}
// Accepted solution for LeetCode #926: Flip String to Monotone Increasing
function minFlipsMonoIncr(s: string): number {
let tot = 0;
for (const c of s) {
tot += c === '0' ? 1 : 0;
}
let [ans, cur] = [tot, 0];
for (let i = 1; i <= s.length; ++i) {
cur += s[i - 1] === '0' ? 1 : 0;
ans = Math.min(ans, i - cur + tot - cur);
}
return ans;
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(n × m)
Space
O(n × m)
Approach Breakdown
RECURSIVE
O(2ⁿ) time
O(n) space
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
DYNAMIC PROGRAMMING
O(n × m) time
O(n × m) space
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Shortcut: Count your DP state dimensions → that’s your time. Can you drop one? That’s your space optimization.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
State misses one required dimension
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.