-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
Copy pathCarFleetII.java
28 lines (28 loc) · 1.01 KB
/
CarFleetII.java
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
class Solution {
public double[] getCollisionTimes(int[][] cars) {
int length = cars.length;
double[] times = new double[length];
Deque<Integer> stack = new LinkedList<Integer>();
for (int i = length - 1; i >= 0; i--) {
while (!stack.isEmpty()) {
if (cars[stack.peek()][1] >= cars[i][1])
stack.pop();
else {
if (times[stack.peek()] < 0)
break;
double time = times[stack.peek()] * (cars[i][1] - cars[stack.peek()][1]);
if (time > cars[stack.peek()][0] - cars[i][0])
break;
else
stack.pop();
}
}
if (stack.isEmpty())
times[i] = -1;
else
times[i] = (double) (cars[stack.peek()][0] - cars[i][0]) / (cars[i][1] - cars[stack.peek()][1]);
stack.push(i);
}
return times;
}
}