Posts Car Fleet
Post
Cancel

Car Fleet

PROBLEM DESCRIPTION

There are n cars going to the same destination along a one-lane road. The destination is target miles away.

You are given two integer array position and speed, both of length n, where position[i] is the position of the ith car and speed[i] is the speed of the ith car (in miles per hour).

A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper at the same speed. The faster car will slow down to match the slower car’s speed. The distance between these two cars is ignored (i.e., they are assumed to have the same position).

A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.

If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.

Return the number of car fleets that will arrive at the destination.

leetcode

SOLUTION

snapshot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {

    public int carFleet(int target, int[] position, int[] speed) {

        int n = position.length;

        //init list of cars
        List<Car> cars = new ArrayList<>();
        for(int i=0; i<n; i++){
            cars.add(new Car(position[i], speed[i]));
        }

        //sort them by position
        Collections.sort(cars);

        //init
        int fleetCount = 0;
        double lastTimeToReachDestination = -1;

        //loop from right most position towards left
        for(int i=n-1; i>=0; i--){

            //time required for current car to reach destination
            double time = (double)(target-cars.get(i).position)/cars.get(i).speed;

            //If the time required by the current car is more than the time taken by previous cars, this will start a new fleet
            if(time > lastTimeToReachDestination){
                fleetCount++;
                lastTimeToReachDestination = time;
            }
        }

        return fleetCount;

    }
}

class Car implements Comparable<Car>{

    int position;
    int speed;

    public Car(int p, int s){
        this.position = p;
        this.speed = s;
    }

    public int compareTo(Car c){

        if(this.position > c.position){
            return 1;
        }else{
            return -1;
        }

    }

}

ANOTHER WAY TO CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {

    public int carFleet(int target, int[] position, int[] speed) {

        int n = position.length;

        // create 2D array with [position, speed]
        int[][] arr = new int[n][2];
        for(int i=0; i<n; i++){
            arr[i][0] = position[i];
            arr[i][1] = speed[i];
        }

        // sort by position
        Arrays.sort(arr, (a, b) -> a[0] - b[0]);

        int fleets = 0;
        double minTime = -1;

        for(int i=n-1; i>=0; i--){

            double timeNeeded = (double)(target - arr[i][0])/(arr[i][1]);

            if(timeNeeded > minTime){
                minTime = timeNeeded;
                fleets++;
            }

        }

        return fleets;

    }

}
This post is licensed under CC BY 4.0 by the author.