Skip to content

Commit 5b8d364

Browse files
authored
Add files via upload
Java solution to problem 853 Car Fleet
1 parent 5841631 commit 5b8d364

File tree

1 file changed

+43
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)