Skip to content

Instantly share code, notes, and snippets.

@amarjitdhillon
Last active February 8, 2022 22:56
Show Gist options
  • Save amarjitdhillon/bbfbdde7a00fda31e70654d5c857e771 to your computer and use it in GitHub Desktop.
Save amarjitdhillon/bbfbdde7a00fda31e70654d5c857e771 to your computer and use it in GitHub Desktop.
Flip String to Monotone Increasing
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