Thursday, March 4, 2021

[Google Question][LeetCode] Car Fleet

Problem: N cars are going to the same destination along a one lane road.  The destination is target miles away.

Each car i has a constant speed speed[i] (in miles per hour), and initial position position[i] miles towards the target along the road.

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 distance between these two cars is ignored - 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.

How many car fleets will arrive at the destination?

Example:

Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Explanation:
The cars starting at 10 and 8 become a fleet, meeting each other at 12.
The car starting at 0 doesn't catch up to any other car, so it is a fleet by itself.
The cars starting at 5 and 3 become a fleet, meeting each other at 6.
Note that no other cars meet these fleets before the destination, so the answer is 3.


Approach: Basically here we need to find out if two cars will collide or not in the given distance. The distance to travel for each car is target - car's position right. Now a car can collide to another car if it is right behind it and if it takes lesser time than next positioned car.

Time to travel for a car[i] = (target - position[i]) / speed[i]. Right! as travel time is equal to distance / speed. 

Now the problem is easy we can get the time travel for each car and then sort the cars based on its position. We can then loop through i  = n - 1 to 1 and follow the below steps:

IF cars[i].TravelTime < cars[i - 1].TravelTime // no collision

result = result + 1

ELSE 

//collision, assign the current car's travel time to previous car's travel time as previous car is forced to take this much of time

cars[i - 1].TravelTime = cars[i].TravelTime

After the loop we will just increment the result by 1 for the 1st car.

That's all!


Implementation in C#:

    public int CarFleet(int target, int[] position, int[] speed) 

    {

        int numOfCars = position.Length;

        if (numOfCars == 0)

        {

            return 0;

        }

        Tuple<int, double>[] cars = new Tuple<int, double>[numOfCars];

        for (int i = 0; i < numOfCars; ++i)

        {

            cars[i] = new Tuple<int, double>(position[i], (double)(target - position[i]) / speed[i]);

        }

        Array.Sort(cars, (c1, c2) => {

           return c1.Item1.CompareTo(c2.Item1); 

        });

        int numOfFleets = 0;

        for (int i = numOfCars - 1; i > 0; --i)

        {

            if (cars[i].Item2 < cars[i - 1].Item2)

            {

                ++numOfFleets;

            }

            else

            {

                cars[i - 1] = cars[i];

            }

        }

        return numOfFleets + 1;

    }


Complexity: O(nlogn)

No comments:

Post a Comment