给一串由0和1组成的字符串,每次操作把其中一位从0变1或者从1变0,问至少几次操作可以让整个字符串变成从左至右单调增
参考了Discussion的巧妙思路->https://leetcode.com/problems/flip-string-to-monotone-increasing/solutions/3061225/python-3-7-lines-w-explanation-and-example-t-m-100-96/
1 #926
2 #Runtime: 92 ms (Beats 95.45%)
3 #Memory: 14.6 MB (Beats 95.45%)
4
5 class Solution(object):
6 def minFlipsMonoIncr(self, s):
7 """
8 :type s: str
9 :rtype: int
10 """
11 t, ans = 0, 0
12 for i in s:
13 if i == '1':
14 t += 1
15 elif t:
16 ans += 1
17 t -= 1
18 return ans
19