Skip to content

Commit 3e65329

Browse files
authored
Create 926-flip-string-to-monotone-increasing.js
1 parent e33725c commit 3e65329

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const minFlipsMonoIncr = function(s) {
6+
const n = s.length
7+
const arr = Array(n).fill(0)
8+
let oneCnt = 0
9+
for(let i = 0; i < n; i++) {
10+
if(s[i] === '1') oneCnt++
11+
arr[i] = oneCnt
12+
}
13+
const zeroCnt = n - oneCnt
14+
let res = Infinity
15+
16+
for(let i = 0; i < n; i++) {
17+
const cnt = arr[i]
18+
const tmp = cnt + (zeroCnt - (i + 1 - cnt))
19+
res = Math.min(res, tmp)
20+
}
21+
res = Math.min(res, oneCnt, zeroCnt)
22+
return res
23+
};

0 commit comments

Comments
 (0)