There are n cars traveling at different speeds in the same direction along a one-lane road. You are given an array cars of length n, where cars[i] = [positioni, speedi] represents:
positioni is the distance between the ith car and the beginning of the road in meters. It is guaranteed that positioni < positioni+1.
speedi is the initial speed of the ith car in meters per second.
For simplicity, cars can be considered as points moving along the number line.
Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the slowest car in the fleet.
Return an array answer, where answer[i] is the time, in seconds, at which the ith car collides with the next car, or -1 if the car does not collide with the next car. Answers within 10-5 of the actual answers are accepted.
Input: cars =[[1,2],[2,1],[4,3],[7,2]]Output: [1.00000,-1.00000,3.00000,-1.00000]Explanation: After exactly one second, the first car will collide with the second car, and form a car fleet with speed 1 m/s. After exactly 3 seconds, the third car will collide with the fourth car, and form a car fleet with speed 2 m/s.
We process cars from right to left, using a stack to keep track of possible collision candidates. For each car, we compute the time it would take to collide with the car ahead. If the car ahead will collide with another car before our current car can catch up, we skip it. This ensures we only consider valid collision times.
classSolution {
public: vector<double> getCollisionTimes(vector<vector<int>>& cars) {
int n = cars.size();
vector<double> ans(n, -1.0);
stack<int> st;
for (int i = n -1; i >=0; --i) {
int p = cars[i][0], s = cars[i][1];
while (!st.empty()) {
int j = st.top();
int pj = cars[j][0], sj = cars[j][1];
if (s <= sj) {
st.pop();
} else {
double t = (pj - p) *1.0/ (s - sj);
if (ans[j] <0|| t <= ans[j]) {
ans[i] = t;
break;
} else {
st.pop();
}
}
}
st.push(i);
}
return ans;
}
};
classSolution {
publicdouble[]getCollisionTimes(int[][] cars) {
int n = cars.length;
double[] ans =newdouble[n];
Arrays.fill(ans, -1.0);
Deque<Integer> st =new ArrayDeque<>();
for (int i = n - 1; i >= 0; i--) {
int p = cars[i][0], s = cars[i][1];
while (!st.isEmpty()) {
int j = st.peek();
int pj = cars[j][0], sj = cars[j][1];
if (s <= sj) {
st.pop();
} else {
double t = (pj - p) * 1.0/ (s - sj);
if (ans[j]< 0 || t <= ans[j]) {
ans[i]= t;
break;
} else {
st.pop();
}
}
}
st.push(i);
}
return ans;
}
}
classSolution {
fungetCollisionTimes(cars: Array<IntArray>): DoubleArray {
val n = cars.size
val ans = DoubleArray(n) { -1.0 }
val st = ArrayDeque<Int>()
for (i in n - 1 downTo 0) {
val(p, s) = cars[i]
while (st.isNotEmpty()) {
val j = st.last()
val(pj, sj) = cars[j]
if (s <= sj) {
st.removeLast()
} else {
val t = (pj - p).toDouble() / (s - sj)
if (ans[j] < 0|| t <= ans[j]) {
ans[i] = t
break } else {
st.removeLast()
}
}
}
st.addLast(i)
}
return ans
}
}
classSolution:
defgetCollisionTimes(self, cars: list[list[int]]) -> list[float]:
n = len(cars)
ans = [-1.0] * n
st = []
for i in range(n -1, -1, -1):
p, s = cars[i]
while st:
j = st[-1]
pj, sj = cars[j]
if s <= sj:
st.pop()
else:
t = (pj - p) / (s - sj)
if ans[j] <0or t <= ans[j]:
ans[i] = t
breakelse:
st.pop()
st.append(i)
return ans
impl Solution {
pubfnget_collision_times(cars: Vec<Vec<i32>>) -> Vec<f64> {
let n = cars.len();
letmut ans =vec![-1.0; n];
letmut st = Vec::new();
for i in (0..n).rev() {
let (p, s) = (cars[i][0], cars[i][1]);
whilelet Some(&j) = st.last() {
let (pj, sj) = (cars[j][0], cars[j][1]);
if s <= sj {
st.pop();
} else {
let t = (pj - p) asf64/ (s - sj) asf64;
if ans[j] <0.0|| t <= ans[j] {
ans[i] = t;
break;
} else {
st.pop();
}
}
}
st.push(i);
}
ans
}
}