Minimum number of flips to make a Binary String increasing | Function to find the minimum number of flips required to make string increasing ; Length of s ; Total number of zero in s ; Stores count of 1 s till ith index ; Stores the minimum count of flips ; Traverse the given string S ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Given String ; Function Call
def minimumFlips ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE cnt0 = s . count ( '0' ) NEW_LINE cnt1 = 0 NEW_LINE res = n - cnt0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if s [ i ] == '0' : NEW_LINE INDENT cnt0 -= 1 NEW_LINE DEDENT elif s [ i ] == '1' : NEW_LINE INDENT res = min ( res , cnt1 + cnt0 ) NEW_LINE cnt1 += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT S = '000110' NEW_LINE print ( minimumFlips ( S ) ) NEW_LINE