Skip to content

Commit 1f42a10

Browse files
committed
98
1 parent a260422 commit 1f42a10

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

String/926.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Flip String to Monotone Increasing
2+
3+
#### Description
4+
5+
[link](https://leetcode.com/problems/flip-string-to-monotone-increasing/discuss/184080/Python-3-liner)
6+
7+
---
8+
9+
#### Solution
10+
11+
- We start with assuming "111.." section occupies all string, s.
12+
- Then we update "000.." section as s[:i + 1] and "111.." section as s[i + 1:] during iteration as well as the result
13+
- "zeros" variable counts all misplaced "0"s and "ones" variable counts all misplaced "1"s
14+
15+
---
16+
17+
#### Code
18+
19+
O(n)
20+
21+
```python
22+
class Solution:
23+
def minFlipsMonoIncr(self, s: str) -> int:
24+
ones = 0
25+
res = zeros = s.count("0")
26+
for c in s:
27+
ones, zeros = (ones + 1, zeros) if c == "1" else (ones, zeros - 1)
28+
res = min(res, ones + zeros)
29+
return res
30+
```

0 commit comments

Comments
 (0)