Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c105d97

Browse files
authored
Create 853. Car Fleet.java
1 parent 75d5203 commit c105d97

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

java/853. Car Fleet.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)