File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments