Last active
February 8, 2022 22:56
-
-
Save amarjitdhillon/bbfbdde7a00fda31e70654d5c857e771 to your computer and use it in GitHub Desktop.
Flip String to Monotone Increasing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def minFlipsMonoIncr(self, s: str) -> int: | |
oneCount, flipCount = 0,0 # intially both are 0 | |
for c in s: | |
if c == "1": | |
oneCount += 1 | |
else: # if not 1 then it's a zero | |
if oneCount >= 1: | |
flipCount += 1 # in case we have already seen a zero, we will increase the flipCount | |
flipCount = min(flipCount, oneCount) # we will do fip if it's count is less then # of 1's seen so far | |
return flipCount | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment