Skip to content

Commit 7c60543

Browse files
authored
Create flip-string-to-monotone-increasing.cpp
1 parent 2e4a3df commit 7c60543

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minFlipsMonoIncr(string S) {
7+
int flip0 = 0, flip1 = 0;
8+
for (const auto& c : S) {
9+
flip0 += static_cast<int>(c == '1');
10+
flip1 = min(flip0, flip1 + static_cast<int>(c == '0'));
11+
}
12+
return flip1;
13+
}
14+
};

0 commit comments

Comments
 (0)