926. Flip String to Monotone Increasing

https://leetcode.com/problems/flip-string-to-monotone-increasing/

解答:

class Solution:
    def minFlipsMonoIncr(self, S: str) -> int:
        zero = S.count("0")
        one = 0
        res = len(S) - zero
        for i in S:
            if i == "0":
                zero-=1
            else:
                res = min(res,zero+one)
                one+=1
        return res

Last updated

Was this helpful?