Problem Solving/LeetCode
2023.01.17 Today's Challenge
fabichoi
2023. 1. 17. 23:45
https://leetcode.com/problems/flip-string-to-monotone-increasing/
Flip String to Monotone Increasing - LeetCode
Flip String to Monotone Increasing - A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none). You are given a binary string s. You can flip s[i] changing it from 0 to
leetcode.com
뒤에서부터가 아니라, 앞에서부터 하나씩 체크하면 되는 문제
class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
ones, res = 0, 0
for num in s:
if num == '1':
ones += 1
elif ones:
ones -= 1
res += 1
return res
반응형