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:
Remove a train car from the left end (i.e., remove s[0]) which takes 1 unit of time.
Remove a train car from the right end (i.e., remove s[s.length - 1]) which takes 1 unit of time.
Remove a train car from anywhere in the sequence which takes 2 units of time.
Return theminimum 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.
Input: s ="**_11_** 00** _1_** 0** _1_** "Output: 5Explanation:
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 is2*1=2.- remove a car from the right end. Time taken is1.- remove the car containing illegal goods found in the middle. Time taken is2.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 is2*1=2.- remove a car from the right end 3 times. Time taken is3*1=3.This also obtains a total time of 2+3=5.5is the minimum time taken to remove all the cars containing illegal goods.There are no other ways to remove them with less time.
Input: s ="00** _1_** 0"Output: 2Explanation:
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 is3*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 is2.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 is2*1=2.This obtains a total time of 2.2is the minimum time taken to remove all the cars containing illegal goods.There are no other ways to remove them with less time.
We can remove cars from the left, right, or anywhere (with different costs). For each position, we can compute the minimum cost to remove all illegal cars to the left and right, and combine them for the optimal solution.
classSolution {
public:int minimumTime(string s) {
int n = s.size();
vector<int> pre(n), suf(n);
pre[0] = s[0] =='1'?1:0;
for (int i =1; i < n; ++i) {
if (s[i] =='1')
pre[i] = min(pre[i-1] +1, i +1);
else pre[i] = pre[i-1];
}
suf[n-1] = s[n-1] =='1'?1:0;
for (int i = n-2; i >=0; --i) {
if (s[i] =='1')
suf[i] = min(suf[i+1] +1, n - i);
else suf[i] = suf[i+1];
}
int ans = suf[0];
for (int i =0; i < n-1; ++i)
ans = min(ans, pre[i] + suf[i+1]);
ans = min(ans, pre[n-1]);
return ans;
}
};
classSolution {
publicintminimumTime(String s) {
int n = s.length();
int[] pre =newint[n], suf =newint[n];
pre[0]= s.charAt(0) =='1'? 1 : 0;
for (int i = 1; i < n; i++) {
if (s.charAt(i) =='1')
pre[i]= Math.min(pre[i-1]+ 1, i + 1);
else pre[i]= pre[i-1];
}
suf[n-1]= s.charAt(n-1) =='1'? 1 : 0;
for (int i = n-2; i >= 0; i--) {
if (s.charAt(i) =='1')
suf[i]= Math.min(suf[i+1]+ 1, n - i);
else suf[i]= suf[i+1];
}
int ans = suf[0];
for (int i = 0; i < n-1; i++)
ans = Math.min(ans, pre[i]+ suf[i+1]);
ans = Math.min(ans, pre[n-1]);
return ans;
}
}
classSolution {
funminimumTime(s: String): Int {
val n = s.length
val pre = IntArray(n)
val suf = IntArray(n)
pre[0] = if (s[0] =='1') 1else0for (i in1 until n) {
pre[i] = if (s[i] =='1') minOf(pre[i-1]+1, i+1) else pre[i-1]
}
suf[n-1] = if (s[n-1] =='1') 1else0for (i in n-2 downTo 0) {
suf[i] = if (s[i] =='1') minOf(suf[i+1]+1, n-i) else suf[i+1]
}
var ans = suf[0]
for (i in0 until n-1) {
ans = minOf(ans, pre[i]+suf[i+1])
}
ans = minOf(ans, pre[n-1])
return ans
}
}
classSolution:
defminimumTime(self, s: str) -> int:
n: int = len(s)
pre: list[int] = [0] * n
suf: list[int] = [0] * n
pre[0] =1if s[0] =='1'else0for i in range(1, n):
if s[i] =='1':
pre[i] = min(pre[i-1]+1, i+1)
else:
pre[i] = pre[i-1]
suf[n-1] =1if s[n-1] =='1'else0for i in range(n-2, -1, -1):
if s[i] =='1':
suf[i] = min(suf[i+1]+1, n-i)
else:
suf[i] = suf[i+1]
ans: int = suf[0]
for i in range(n-1):
ans = min(ans, pre[i]+suf[i+1])
ans = min(ans, pre[n-1])
return ans