You are given a 0-indexed binary string s
which represents a sequence of train cars. s[i] = '0'
denotes that the ith
car does not contain illegal goods and s[i] = '1'
denotes that the ith
car does contain illegal goods.
As the train conductor, you would like to get rid of all the cars containing illegal goods. You can do any of the following three operations any number of times:
s[0]
) which takes 1 unit of time.s[s.length - 1]
) which takes 1 unit of time.Return the minimum time to remove all the cars containing illegal goods.
Note that an empty sequence of cars is considered to have no cars containing illegal goods.
Example 1:
Input: s = "1100101" Output: 5 Explanation: One way to remove all the cars containing illegal goods from the sequence is to - remove a car from the left end 2 times. Time taken is 2 * 1 = 2. - remove a car from the right end. Time taken is 1. - remove the car containing illegal goods found in the middle. Time taken is 2. This obtains a total time of 2 + 1 + 2 = 5. An alternative way is to - remove a car from the left end 2 times. Time taken is 2 * 1 = 2. - remove a car from the right end 3 times. Time taken is 3 * 1 = 3. This also obtains a total time of 2 + 3 = 5. 5 is the minimum time taken to remove all the cars containing illegal goods. There are no other ways to remove them with less time.
Example 2:
Input: s = "0010" Output: 2 Explanation: One way to remove all the cars containing illegal goods from the sequence is to - remove a car from the left end 3 times. Time taken is 3 * 1 = 3. This obtains a total time of 3. Another way to remove all the cars containing illegal goods from the sequence is to - remove the car containing illegal goods found in the middle. Time taken is 2. This obtains a total time of 2. Another way to remove all the cars containing illegal goods from the sequence is to - remove a car from the right end 2 times. Time taken is 2 * 1 = 2. This obtains a total time of 2. 2 is the minimum time taken to remove all the cars containing illegal goods. There are no other ways to remove them with less time.
Constraints:
1 <= s.length <= 2 * 105
s[i]
is either '0'
or '1'
.This problem asks for the minimum time to remove all cars with illegal goods ('1' in the binary string s
). We can remove cars from the left or right end (cost 1) or from anywhere (cost 2).
The optimal solution involves a dynamic programming-like approach using prefix and suffix arrays.
Prefix Array (pre
): For each index i
, pre[i+1]
stores the minimum time to remove all illegal cars from s[0...i]
. If s[i]
is '0', it's the same as pre[i]
. If s[i]
is '1', we can either remove cars from the left (cost i+1
) or remove all before it and the current car (cost pre[i] + 2
).
Suffix Array (suf
): Similarly, suf[i]
stores the minimum time to remove all illegal cars from s[i...n-1]
.
Combining Prefix and Suffix: The minimum time to remove all illegal cars is the minimum of pre[i] + suf[i]
for all i
from 1 to n
. This represents splitting the string at index i
and removing the left part using the prefix array and the right part using the suffix array.
pre
and suf
, each of size O(n).class Solution:
def minimumTime(self, s: str) -> int:
n = len(s)
pre = [0] * (n + 1) # Prefix array
suf = [0] * (n + 1) # Suffix array
# Populate prefix array
for i, c in enumerate(s):
pre[i + 1] = pre[i] if c == '0' else min(pre[i] + 2, i + 1)
# Populate suffix array
for i in range(n - 1, -1, -1):
suf[i] = suf[i + 1] if s[i] == '0' else min(suf[i + 1] + 2, n - i)
# Find minimum time by combining prefix and suffix
min_time = float('inf')
for i in range(1, n + 1):
min_time = min(min_time, pre[i] + suf[i])
return min_time
The code in other languages (Java, C++, Go) follows the same logic with minor syntax differences, maintaining the same time and space complexity. The min
function is used extensively to determine the minimum cost at each step based on the options of removing from the left, right, or middle.