File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
solution/0800-0899/0853.Car Fleet Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ class carState
2
+ {
3
+ public int distance ;
4
+ public int speed ;
5
+ public double time ;
6
+
7
+ public carState (int distance , int speed , double time )
8
+ {
9
+ this .distance =distance ;
10
+ this .speed =speed ;
11
+ this .time =time ;
12
+ }
13
+ }
14
+
15
+ class Solution {
16
+
17
+ public int carFleet (int target , int [] position , int [] speed ) {
18
+ int l =position .length ;
19
+ carState cars []=new carState [l ];
20
+ int i ;
21
+ for (i =0 ;i <l ;i ++)
22
+ {
23
+ cars [i ]=new carState (target -position [i ],speed [i ],((double )(target -position [i ]))/(double )(speed [i ]));
24
+ }
25
+ Arrays .sort (cars , new Comparator <carState >() {
26
+ public int compare (carState state1 , carState state2 ) {
27
+ return state1 .distance -state2 .distance ;
28
+ }
29
+ });
30
+
31
+ int ans =0 ;
32
+ double currTime =0.0 ;
33
+ for (i =0 ;i <cars .length ;i ++)
34
+ {
35
+ if (cars [i ].time >currTime )
36
+ {
37
+ ans ++;
38
+ currTime =cars [i ].time ;
39
+ }
40
+ }
41
+ return ans ;
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments