Leetcode: 2167. Minimum Time to Remove All Cars Containing Illegal Goods

Problem Statement

class Solution:
    def minimumTime(self, s: str) -> int:
        N = len(s)
        best = 0
        ans = float("inf")
        j = N - 1
        for i in range(N - 1, -1, -1):
            if i == j:
                j -= 1
                while j >= 0 and s[j] == "0":
                    j -= 1
            best = min(N - i, 2 + best if s[i] == "1" else best)
            ans = min(ans, j + 1 + best)
        return ans


assert Solution().minimumTime("1100101") == 5
assert Solution().minimumTime("0010") == 2