class Solution {
    public int minFlipsMonoIncr(String s) {
        int oneNum = 0, flipNum =0;
        int res =0;
        for(char c: s.toCharArray()){
            if(c=='0'){
                if(oneNum==0)
                    continue;
                else{
                    flipNum++;
                }
            }else{
                oneNum++;
            }
            if(flipNum>oneNum){  //if fliped 0's number is larger than 1's number, 
                res+=oneNum;   //Then we prefer to flip 1s
                flipNum=0;     
                oneNum=0;
            }
        }
        return res+flipNum;   //After the loop, add fliped 1s and fliped 0s
    }
}

 

posted on 2022-03-22 06:09  阳光明媚的菲越  阅读(12)  评论(0编辑  收藏  举报