Leetcode 962 Solution

This article provides solution to leetcode question 962 (flip-string-to-monotone-increasing)

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

Solution

class Solution:
    def minFlipsMonoIncr(self, S: str) -> int:
        cur1 = 0
        cur2 = 0
        for i, ch in enumerate(S):
            if i == 0:
                cur2 = 1 if ch == '0' else 0
            else:
                cur2 = min(cur2, cur1) + (1 if ch == '0' else 0)
            cur1 += 1 if ch == '1' else 0

        return min(cur1, cur2)