926 Flip String to Monotone Increasing

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

solution

class Solution:
    def minFlipsMonoIncr(self, s: str) -> int:
        dp = 0
        count1 = 0

        for c in s:
            if c == '0':
                # 1. Flip '0'.
                # 2. Keep '0' and flip all the previous 1s.
                dp = min(dp + 1, count1)
            else:
                count1 += 1
        return dp

时间复杂度:O(n) 空间复杂度:O(1)

follow up

1653. Minimum Deletions to Make String Balanced

Last updated