Skip to content

Add files via upload #1443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions solution/0800-0899/0853.Car Fleet/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class carState
{
public int distance;
public int speed;
public double time;

public carState(int distance, int speed, double time)
{
this.distance=distance;
this.speed=speed;
this.time=time;
}
}

class Solution {

public int carFleet(int target, int[] position, int[] speed) {
int l=position.length;
carState cars[]=new carState[l];
int i;
for(i=0;i<l;i++)
{
cars[i]=new carState(target-position[i],speed[i],((double)(target-position[i]))/(double)(speed[i]));
}
Arrays.sort(cars, new Comparator<carState>() {
public int compare(carState state1, carState state2) {
return state1.distance-state2.distance;
}
});

int ans=0;
double currTime=0.0;
for(i=0;i<cars.length;i++)
{
if(cars[i].time>currTime)
{
ans++;
currTime=cars[i].time;
}
}
return ans;
}
}