We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 0a6c754 + 1fc2f4d commit 0942599Copy full SHA for 0942599
java/853-Car Fleet.java
@@ -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