Car Fleet - Complete Solution Guide
Car Fleet is LeetCode problem 853, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
There are n cars at given miles away from the starting mile 0, traveling to reach the mile target . You are given two integer arrays position and speed , both of length n , where position[i] is the starting mile of the i th car and speed[i] is the speed of the i th car in miles per hour. A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car. A car fleet is a single car or a group of cars driving next to each other. The speed of the car fleet
Detailed Explanation
The problem describes a scenario with multiple cars moving towards a target destination. Each car has a starting position and speed. Cars can form fleets, where a car catches up to another and they travel together at the speed of the slower car. The goal is to determine the number of car fleets that will reach the target.
Solution Approach
The provided solution sorts the cars by their initial positions in descending order. It then iterates through the sorted cars, calculating the arrival time for each car to reach the target. If a car's arrival time is greater than the current fleet's arrival time, it means this car forms a new fleet. Otherwise, it joins the existing fleet. The solution maintains a count of the number of fleets formed.
Step-by-Step Algorithm
- Step 1: Create a list of car objects (or pairs) containing position and speed for each car.
- Step 2: Sort the list of cars in descending order based on their position. Cars closer to the target come first.
- Step 3: Initialize a variable `fleets` to 0, to store the number of fleets.
- Step 4: Initialize a variable `current_fleet_arrival_time` to 0.0, representing the arrival time of the latest formed fleet.
- Step 5: Iterate through the sorted list of cars.
- Step 6: For each car, calculate its arrival time as `(target - position) / speed`.
- Step 7: Compare the car's arrival time with `current_fleet_arrival_time`.
- Step 8: If the car's arrival time is greater than `current_fleet_arrival_time`, it means the car will not join the current fleet, so increment `fleets` by 1 and update `current_fleet_arrival_time` to the car's arrival time.
- Step 9: Return the final `fleets` count.
Key Insights
- Insight 1: Sorting the cars by their starting positions in reverse order simplifies the fleet detection process. By processing cars from closest to the target to farthest, we can easily determine if a car will join an existing fleet.
- Insight 2: The key factor determining fleet formation is the time it takes for each car to reach the target. If a car's arrival time is less than or equal to the current fleet's arrival time, it will join that fleet.
- Insight 3: Since cars cannot pass each other, we only need to track the arrival time of the slowest car (the one that arrives latest) in the current fleet. A new fleet is formed only when the current car takes longer to reach the target than the current fleet's arrival time.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Stack, Sorting, Monotonic Stack.
Companies
Asked at: BNY Mellon, BharatPe, GE Healthcare, Infosys, Nutanix, Snap.