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 } }