File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int carFleet (int target , int [] position , int [] speed ) {
3
+ double [][] mergedPoints = new double [position .length ][2 ];
4
+ for (int idx =0 ;idx <position .length ;idx ++){
5
+ mergedPoints [idx ][0 ] = position [idx ];
6
+ mergedPoints [idx ][1 ] = speed [idx ];
7
+ }
8
+ Arrays .sort (mergedPoints , Comparator .comparingDouble (o -> -o [0 ]));
9
+ Stack <Double > backup = new Stack ();
10
+ for (int idx =0 ;idx <position .length ;idx ++){
11
+
12
+ double _position = mergedPoints [idx ][0 ];
13
+ double _speed = mergedPoints [idx ][1 ];
14
+
15
+ //System.out.println("_position : " + _position);
16
+
17
+ double _timeTaken = (target -_position )/_speed ;
18
+ double _lastValue = 0 ;
19
+ if (!backup .isEmpty ()){
20
+ _lastValue = backup .peek ();
21
+ }
22
+ backup .push (_timeTaken );
23
+ //System.out.println(_timeTaken +"<="+ _lastValue +" is true :" + (_timeTaken <= _lastValue));
24
+ if (backup .size ()>1 &&
25
+ _timeTaken <= _lastValue ){
26
+ backup .pop ();
27
+ }
28
+ }
29
+ return backup .size ();
30
+
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments