Skip to content

Commit 11b204b

Browse files
committed
Create: 0926-flip-string-to-monotone-increasing.rs / .ts / .js /.go
1 parent ee7a3bc commit 11b204b

4 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func minFlipsMonoIncr(s string) int {
2+
res, countOne := 0, 0
3+
4+
for _, ch := range s {
5+
if ch == '1' {
6+
countOne++
7+
} else {
8+
res = min(res+1, countOne)
9+
}
10+
}
11+
12+
return res
13+
}
14+
15+
func min(a, b int) int {
16+
if a < b {
17+
return a
18+
}
19+
return b
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var minFlipsMonoIncr = function (s) {
6+
let [res, countOne] = [0, 0];
7+
8+
for (ch of s) {
9+
if (ch == '1') {
10+
countOne++;
11+
} else {
12+
res = Math.min(res + 1, countOne);
13+
}
14+
}
15+
16+
return res;
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn min_flips_mono_incr(s: String) -> i32 {
3+
let (mut res, mut count_one) = (0, 0);
4+
5+
for ch in s.chars() {
6+
if ch == '1' {
7+
count_one += 1;
8+
} else {
9+
res = i32::min(res + 1, count_one);
10+
}
11+
}
12+
res
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minFlipsMonoIncr(s: string): number {
2+
let res: number = 0;
3+
let countOne: number = 0;
4+
5+
for (let ch of s) {
6+
if (ch == '1') {
7+
countOne++;
8+
} else {
9+
res = Math.min(res + 1, countOne);
10+
}
11+
}
12+
13+
return res;
14+
}

0 commit comments

Comments
 (0)