We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8f97dfb commit a4989deCopy full SHA for a4989de
scripts/algorithms/F/Flip String to Monotone Increasing/Flip String to Monotone Increasing.java
@@ -1,22 +1,16 @@
1
-// Runtime: 12 ms (Top 74.68%) | Memory: 51.9 MB (Top 43.97%)
2
class Solution {
3
public int minFlipsMonoIncr(String s) {
4
- int count1 =0;
5
- int delete_count =0;
6
- for(char ch: s.toCharArray()){
7
- if(ch=='1'){
8
- if(count1<=0){
9
- count1=1;
10
- }
11
- else{
12
- count1++;
13
14
15
- else if(count1>0){
16
- delete_count++;
17
- count1--;
18
+ int n = s.length();
+ int zeroToOne =0;
+ int countOfOnes=0;
+ for(int i=0;i<n;i++)
+ {
+ if(s.charAt(i)=='0')
+ zeroToOne=Math.min(zeroToOne+1,countOfOnes);
+ else
+ countOfOnes=countOfOnes+1;
19
}
20
- return delete_count;
+ return zeroToOne;
+
21
22
0 commit comments