Skip to content

Commit 0942599

Browse files
authored
Merge pull request neetcode-gh#274 from 1234RHIMANSHU/patch-2
Create 853-Car Fleet.java
2 parents 0a6c754 + 1fc2f4d commit 0942599

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

java/853-Car Fleet.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int carFleet(int target, int[] position, int[] speed) {
3+
if (position.length==1)
4+
return 1;
5+
Stack<Double> stack=new Stack<>();
6+
int[][] combine=new int[position.length][2];
7+
for(int i=0;i<position.length;i++){
8+
combine[i][0]=position[i];
9+
combine[i][1]=speed[i];
10+
}
11+
12+
13+
14+
Arrays.sort(combine,java.util.Comparator.comparingInt(o->o[0]));
15+
for(int i=combine.length-1;i>=0;i--){
16+
double currentTime=(double)(target-combine[i][0])/combine[i][1];
17+
if(!stack.isEmpty() && currentTime<=stack.peek()){
18+
continue;
19+
}
20+
else{
21+
stack.push(currentTime);
22+
}
23+
}
24+
return stack.size();
25+
}
26+
}

0 commit comments

Comments
 (0)