926. Flip String to Monotone Increasing

T: O(n)

S: O(1)

class Solution {
    public int minFlipsMonoIncr(String s) {
        int flips = 0;
        int ones = 0;
        
        for (char c : s.toCharArray()) {
            if (c == '1') {
                ones++;
            } else {
                if (ones != 0) {
                    flips++;
                }
            }
            if (flips > ones) {
                flips = ones;
            }
        }
        return flips;
    }
}

/*
so the last skill is to flip all 1

others see if we have a better solution?

from end to fix 0?


00110

if all zero..pass

2 case -> 
1. cur is zero, and after one, so needs flip
2. flip is over ones -> means we can just flip ones' coount


*/

Last updated