Minimum Time to Remove All Cars Containing Illegal Goods
Problem
You are given a 0-indexed binary string which represents a sequence of train cars. denotes that the car does not contain illegal goods and denotes that the 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 ) which takes unit of time.
- Remove a train car from the right end (i.e., remove ) which takes unit of time.
- Remove a train car from anywhere in the sequence which takes units 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
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.
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
- is either or
Submit your solution at here
Solution
Intuition
- We split the train into 3 parts. Left & Mid & Right
- We apply operation 1 on Left, operation 3 on Mid, and operation 2 on Right
- That naive split will have us ended up with solution, which is unfeasible with
- We will optimize further by split the train into 2 parts. Left and Right. We apply operation 1 or 3 on Left, operation 2 on Right
Approach
- Let is the cost to use operation 1 at position i to clear all car in the range . By that definition since we have to eleminate everything came before i to be able to use operation 1.
- Let is the cost to use operation 2 at position i to clear all car in the range . By that definition
- Let is the cost to use operation 3 at position i to clear all illegal car in the range .
- Let’s say we are at car and up until car we know the cost to clear all illegal car using method 1 is and using method 3 is then
- Loop through all middle position and calculate the cost if we were to split the train at . We use operation 1 and 3 on the left part and operation 2 on the right part.
Complexity
- Time complexity:
- Space complexity: , but can be optimized to
Code
impl Solution {
pub fn minimum_time(s: String) -> i32 {
use std::cmp::min;
let n = s.len();
let mut f = vec![vec![0;n+1];4];
// f(1,i) = cost to use operation 1 at i~n
// f(2,i) = cost to use operation 2 at 0~i
// f(3,i) = min cost to use operation 3 at 0~i
let mut ret = n;
for (i,c) in s.chars().enumerate() {
f[2][i+1] = n-i-1;
if c == '1' {
f[3][i+1] = min(f[1][i], f[3][i]) + 2;
f[1][i+1] = i+1;
} else {
f[3][i+1] = f[3][i];
f[1][i+1] = f[1][i];
}
ret = min(ret, min(f[1][i+1], f[3][i+1]) + f[2][i+1]);
}
ret as i32
}
}
space solution
impl Solution {
pub fn minimum_time(s: String) -> i32 {
use std::cmp::min;
let n = s.len();
let mut pick_mid = 0;
let mut clear_left = 0;
let mut ret = n;
for (i,c) in s.chars().enumerate() {
let clear_right = n-i-1;
if c == '1' {
pick_mid = min(clear_left, pick_mid) + 2;
clear_left = i+1;
}
ret = min(ret, min(pick_mid, clear_left) + clear_right);
}
ret as i32
}
}
Bonus, Go implementation
func minimumTime(s string) int {
n := len(s)
pick_mid := 0
clear_left := 0
ret := n
for i,c := range s {
clear_right := n-i-1
if c == '1' {
pick_mid = min(clear_left, pick_mid) + 2
clear_left = i+1
}
ret = min(ret, min(pick_mid, clear_left) + clear_right)
}
return ret
}