Skip to content

Commit 2c89729

Browse files
add js solution for 926
1 parent 50b939c commit 2c89729

File tree

1 file changed

+17
-0
lines changed
  • solution/926. Flip String to Monotone Increasing

1 file changed

+17
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const minFlipsMonoIncr = function (S) {
2+
let n = S.length;
3+
let f = [];
4+
for (let i = 0; i < n + 1; i++) {
5+
f.push(0);
6+
}
7+
for (let i = 0; i < n; i++) {
8+
f[i + 1] = f[i] + (S[i] === '1');
9+
}
10+
let ans = n;
11+
for (let i = 0; i <= n; i++) {
12+
let a = f[i];
13+
let b = (n - i) - (f[n] - f[i]);
14+
ans = Math.min(ans, b + a);
15+
}
16+
return ans;
17+
};

0 commit comments

Comments
 (0)