Flip String to Monotone Increasing LeetCode solution in Java

1
class Solution {
2
public int minFlipsMonoIncr(String s) {
3
int n = s.length();
4
int zeroToOne = 0;
5
int countOfOnes = 0;
6
for (int i = 0; i < n; i++) {
7
if (s.charAt(i) == '0') zeroToOne = Math.min(zeroToOne + 1, countOfOnes);
8
else countOfOnes = countOfOnes + 1;
9
}
10
return zeroToOne;
11
}
12
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0