Leetcode 962 Solution
This article provides solution to leetcode question 962 (flip-string-to-monotone-increasing)
Access this page by simply typing in "lcs 962" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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)