class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
"""tc O(N) sc O(1)
main idea: DP
imagine for substring s[:i] the format is 0000111, current s[i] is '1' so we dont need to make changes
when current s[i] is '0' , we have two options to make (1) convert current '0' to '1' (2) convert previous all '1' to '0'.
we can keep record of '1's so far and compare which option is optimal at current stage
"""
min_flips = ones = 0
for c in s:
if c == '1':
ones += 1
else:
if ones > 0:
min_flips = min(min_flips+1, ones)
return min_flips