<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:
        if not s: return 0
        len_s = len(s)
        real_start = 0
        while s[real_start] == "0":
            real_start += 1
        
        real_end = len_s - 1
        while real_end >= 0 and s[real_end] == "1":
            real_end -= 1

        prefix_count = [[0, 0]]
        for i in range(real_start, real_end + 1):
            prev_zero_count, prev_one_count = prefix_count[-1]
            prefix_count.append(
                    [prev_zero_count + (1 if s[i] == "0" else 0),
                     prev_one_count + (1 if s[i] == "1" else 0)]
                )
        print(prefix_count)
        min_found = len(prefix_count) - 1
        for curr_zero, curr_one in prefix_count[1:]:
            all_left_zeros = curr_one
            all_right_zeros = prefix_count[-1][1] - curr_one
            all_right_ones = prefix_count[-1][0] - curr_zero # 1-b and 2-a
            min_found = min(min_found, all_left_zeros + all_right_zeros)
            min_found = min(min_found, all_left_zeros + all_right_ones)
            all_left_ones = curr_zero # 2
            min_found = min(min_found, all_right_ones + all_left_ones)
        return min_found

        # hard to look at the last bit as a whole and deduce the number of flips
        ## we can try to find a cutoff point where one side is 0 and the other is 1
        ### at cutoff point, find the combination of 
        # 1) converting left to all 0s
        # 1-a convert all right to 0s
        # 1-b convert all right to 1s
        # 2) converting all left to 1s
        # 2-a) convert all right to 1s
        

        

        

        
36521