-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarFleet.java
More file actions
34 lines (30 loc) · 982 Bytes
/
CarFleet.java
File metadata and controls
34 lines (30 loc) · 982 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
29
30
31
32
33
34
package Stack;
import java.util.ArrayList;
import java.util.List;
/*
853. Car Fleet
*/
public class CarFleet {
public static int carFleet(int target, int[] position, int[] speed) {
if(position.length==0 || speed.length==0) return 0;
int res =0;
double currTime =0;
List<List<Integer>> positionSpeed = new ArrayList<>();
for(int i =0; i< position.length;i++){
List<Integer> temp = new ArrayList<>();
temp.add(position[i]);
temp.add(speed[i]);
positionSpeed.add(temp);
}
positionSpeed.sort((o1, o2) -> o1.get(0).compareTo(o2.get(0)));
for(int i =positionSpeed.size()-1;i>=0;i--){
List<Integer> coord = positionSpeed.get(i);
double destTime = (double)(target-coord.get(0))/ coord.get(1);
if(currTime < destTime){
res++;
currTime= destTime;
}
}
return res;
}
}