-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_853_car_fleet.java
More file actions
28 lines (27 loc) · 868 Bytes
/
Copy path_853_car_fleet.java
File metadata and controls
28 lines (27 loc) · 868 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
25
26
27
28
import java.util.Arrays;
public class _853_car_fleet {
public int carFleet(int target, int[] position, int[] speed) {
Double lastFleetTime = Double.MIN_VALUE;
double[][] cars = new double[position.length][2];
int answer =1;
for(int i =0; i<position.length; i++){
cars[i][0]= position[i];
cars[i][1]= (double)(target - position[i])/speed[i];
}
Arrays.sort(cars, (a,b)->Double.compare(a[0],b[0]));
lastFleetTime = (cars[position.length-1][1]);
if(position.length == 1){
return 1;
}
for(int i=position.length-2; i>=0;i--){
if(lastFleetTime>=cars[i][1]){
continue;
}
else {
answer++;
lastFleetTime = cars[i][1];
}
}
return answer;
}
}