926. Flip String to Monotone Increasing

Updated: 2024-03-14
1 min read
[]

On This Page

LeetCode problem 926

class Solution:
    def minFlipsMonoIncr(self, s: str) -> int:
        n = len(s)
        presum = [0] * (n + 1)
        for i, c in enumerate(s):
            presum[i + 1] = presum[i] + int(c)
        res = presum[-1]
        for i in range(n):
            res = min(res, presum[i] + n - i - (presum[-1] - presum[i]))
        return res