-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0853-car-fleet.java
More file actions
24 lines (22 loc) · 815 Bytes
/
0853-car-fleet.java
File metadata and controls
24 lines (22 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
if (position.length == 1) return 1;
Stack<Double> stack = new Stack<>();
int[][] combine = new int[position.length][2];
for (int i = 0; i < position.length; i++) {
combine[i][0] = position[i];
combine[i][1] = speed[i];
}
Arrays.sort(combine, java.util.Comparator.comparingInt(o -> o[0]));
for (int i = combine.length - 1; i >= 0; i--) {
double currentTime = (double) (target - combine[i][0]) /
combine[i][1];
if (!stack.isEmpty() && currentTime <= stack.peek()) {
continue;
} else {
stack.push(currentTime);
}
}
return stack.size();
}
}