926. Flip String to Monotone Increasing
https://leetcode.com/problems/flip-string-to-monotone-increasing/description/
Python
Dynamic Window
class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
left1, right0 = 0, sum([1 for char in s if char == '0'])
ans = right0
print(left1, right0, ans)
for char in s:
if char == '0':
right0 -= 1
else:
left1 += 1
ans = min(ans, left1+right0)
return ans
Dynamic Window (Official Solution)
class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
count = sum([1 for n in s if n == '0'])
ans = count
for char in s:
if char == '0':
count -= 1
ans = min(ans, count)
else:
count += 1
return ans