-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathCar Fleet II.cpp
29 lines (25 loc) · 1009 Bytes
/
Car Fleet II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
vector<double> getCollisionTimes(vector<vector<int>>& cars) {
vector<double> result(cars.size(), -1.0);
stack<int> mystack;
for (int i = cars.size() - 1; i >= 0; i--) {
// We don't need faster cars behind current car.
while (!mystack.empty() && cars[i][1] <= cars[mystack.top()][1]) {
mystack.pop();
}
while (!mystack.empty()) {
int candidate = mystack.top();
double current_time = 1.0 * (cars[candidate][0] - cars[i][0]) / (cars[i][1] - cars[candidate][1]);
if (current_time <= result[candidate] || result[candidate] < 0) {
result[i] = current_time;
break;
}
// This car will collide with cars after candidate.
mystack.pop();
}
mystack.push(i);
}
return result;
}
};