3
int minFlipsMonoIncr(string s) {4
// assume start making 1 from i th index5
// what number of 0s we need to flip at right side6
// what number of 1s we need to flip at left side7
// for this maintain prefix and suffix10
vector<int> noOfZerosToRight(n, 0); // R to L11
vector<int> noOfOnesToLeft(n, 0); // L to R13
if (s[0] == '1') noOfOnesToLeft[0] = 1;14
for (int i = 1; i < n; i++) {16
noOfOnesToLeft[i] = noOfOnesToLeft[i - 1] + 1;18
noOfOnesToLeft[i] = noOfOnesToLeft[i - 1];21
if (s[n - 1] == '0') noOfZerosToRight[n - 1] = 1;22
for (int i = n - 2; i >= 0; i--) {24
noOfZerosToRight[i] = noOfZerosToRight[i + 1] + 1;26
noOfZerosToRight[i] = noOfZerosToRight[i + 1];29
// starting treating i as partition of 0 and 1:30
// 0 at the lefts and 1 at the rights including i:32
for (int i = 0; i < n; i++) {33
int leftFlips = 0; // when we want all 1s34
int rightFlips = noOfZerosToRight[i];36
if (i - 1 >= 0) leftFlips = noOfOnesToLeft[i - 1];37
ans = min(ans, (leftFlips + rightFlips));40
ans = min(ans, noOfOnesToLeft.back() + 0); // when want all 0s