Skip to content

Commit a4f2e41

Browse files
committed
Runtime: 46 ms (Top 47.95%) | Memory: 20.1 MB (Top 15.69%)
1 parent 8aff171 commit a4f2e41

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 46 ms (Top 47.95%) | Memory: 20.1 MB (Top 15.69%)
12
class Solution {
23
public:
34
int minFlipsMonoIncr(string s) {
@@ -7,34 +8,34 @@ class Solution {
78
// for this maintain prefix and suffix
89

910
int n = s.size();
10-
vector<int> noOfZerosToRight(n,0); // R to L
11-
vector<int> noOfOnesToLeft(n,0); // L to R
11+
vector<int> noOfZerosToRight(n,0); // R to L
12+
vector<int> noOfOnesToLeft(n,0); // L to R
1213

13-
if(s[0] == '1') noOfOnesToLeft[0] = 1;
14+
if(s[0] == '1') noOfOnesToLeft[0] = 1;
1415
for(int i=1;i<n;i++){
1516
if(s[i] == '1') noOfOnesToLeft[i] = noOfOnesToLeft[i-1] + 1;
1617
else noOfOnesToLeft[i] = noOfOnesToLeft[i-1];
1718
}
1819

19-
if(s[n-1] == '0') noOfZerosToRight[n-1] = 1;
20+
if(s[n-1] == '0') noOfZerosToRight[n-1] = 1;
2021
for(int i=n-2;i>=0;i--){
2122
if(s[i] == '0') noOfZerosToRight[i] = noOfZerosToRight[i+1] + 1;
2223
else noOfZerosToRight[i] = noOfZerosToRight[i+1];
2324
}
24-
25+
2526
// starting treating i as partition of 0 and 1:
2627
// 0 at the lefts and 1 at the rights including i:
2728
int ans = 1e9;
2829
for(int i=0;i<n;i++){
29-
int leftFlips = 0; //when we want all 1s
30+
int leftFlips = 0; //when we want all 1s
3031
int rightFlips = noOfZerosToRight[i];
31-
32+
3233
if(i-1 >= 0) leftFlips = noOfOnesToLeft[i-1];
3334
ans = min(ans, (leftFlips + rightFlips));
3435
}
35-
36-
ans = min(ans, noOfOnesToLeft.back() + 0); //when want all 0s
36+
37+
ans = min(ans, noOfOnesToLeft.back() + 0); //when want all 0s
3738
return ans;
3839
}
3940
//O(N) + O(N)
40-
};
41+
};

0 commit comments

Comments
 (0)