<p>A binary string is monotone increasing if it consists of some number of <code>0</code>&#39;s (possibly none), followed by some number of <code>1</code>&#39;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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;00110&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> We flip the last digit to get 00111.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;010110&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> We flip to get 011111, or alternatively 000111.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;00011000&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> We flip to get 00000000.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

class Solution:
    def minFlipsMonoIncr(self, s: str) -> int:
        from_left = []
        from_right = []

        if s[0] == "0":
            from_left.append((1,0))
        else:
            from_left.append((0,1))

        if s[-1] == "0":
            from_right.append((1,0))
        else:
            from_right.append((0,1))

        n = len(s)
        for i in range(1, n):
            prev_count = from_left[-1]
            if s[i] == "0":
                new_count = (prev_count[0] + 1, prev_count[1])
            else:
                new_count = (prev_count[0], prev_count[1] + 1)
            from_left.append(new_count)

        for i in reversed(range(n - 1)):
            prev_count = from_right[-1]
            if s[i] == "0":
                new_count = (prev_count[0] + 1, prev_count[1])
            else:
                new_count = (prev_count[0], prev_count[1] + 1)
            from_right.append(new_count)
        from_right.reverse()

        changed_all_zeroes_to_one = from_left[-1][0]
        changed_all_ones_to_zero = from_left[-1][1]
        result = min(changed_all_zeroes_to_one, changed_all_ones_to_zero)

        for i in range(n - 1):
            change_all_upto_now_to_zero_cost = from_left[i][1]
            change_remaining_to_one = from_right[i+1][0]
            cur_cost = change_all_upto_now_to_zero_cost + change_remaining_to_one
            result = min(result, cur_cost)

        return result
36521