> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Flip String to Monotone Increasing

> Tested Python solution for LeetCode 926 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 926, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/flip-string-to-monotone-increasing/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 926   # by problem number
lcpy gen -s flip_string_to_monotone_increasing   # by problem name
```

## Problem

\<p>A binary string is monotone increasing if it consists of some number of \<code>0\</code>'s (possibly none), followed by some number of \<code>1\</code>'s (also possibly none).\</p>

\<p>You are given a binary string \<code>s\</code>. You can flip \<code>s\[i]\</code> changing it from \<code>0\</code> to \<code>1\</code> or from \<code>1\</code> to \<code>0\</code>.\</p>

\<p>Return \<em>the minimum number of flips to make \</em>\<code>s\</code>\<em> monotone increasing\</em>.\</p>

### Examples

```
Input: s = "00110"
Output: 1
Explanation: We flip the last digit to get 00111.
```

```
Input: s = "010110"
Output: 2
Explanation: We flip to get 011111, or alternatively 000111.
```

```
Input: s = "00011000"
Output: 2
Explanation: We flip to get 00000000.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_string_to_monotone_increasing/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_string_to_monotone_increasing/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(?)
    # Space: O(?)
    def min_flips_mono_increasing(self, s: str) -> int:
        ones = 0
        flips = 0
        for char in s:
            if char == "1":
                ones += 1
            else:
                # Either flip this 0 to 1, or flip all 1s seen so far to 0
                flips = min(flips + 1, ones)
        return flips
```

## Complexity

| Time | Space |
| - | - |
| O(?) | O(?) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.