↓Skip to main content
  1. LeetCode/

LeetCode 853: Car Fleet

·2 mins· ·
LeetCode Daily Medium Array Stack Greedy Simulation Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: Medium First Attempt: 2025-08-10

  • Total time: 00:00.00

Intuition
#

Every car has its arrival time to the target. We can sort cars by their position and iterate from the closest to the farthest from the target. If a car’s arrival time is smaller than or equal to the last car in our stack, it means the current car will catch up and form a fleet with the car ahead of it.

Approach
#

The solution uses a stack-based approach with the following steps:

  1. Calculate arrival times: For each car, calculate the time it takes to reach the target using (target - position) / speed
  2. Sort by position: Sort cars by their position in descending order (closest to target first)
  3. Stack processing: Use a stack to track cars that form fleets
  4. Fleet formation: If a car can catch up to the car ahead (arrival time ≤ previous car), it joins the fleet
class Solution:
    def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
        position_rate = [(position[i], (target - position[i]) / speed[i]) for i in range(len(position))]
        position_rate.sort(key=lambda x: (x[0], -x[1]))
        stack = []

        for position, time_to_end in position_rate:
            while stack and stack[-1][1] <= time_to_end: # means will be catch up
                stack.pop()
            
            stack.append((position, time_to_end))

        return len(stack)

Time Complexity: O(n log n) due to sorting Space Complexity: O(n) for the stack

Findings
#

  • Stack-based approach: Using a stack to track fleets is efficient and intuitive
  • Sorting by position: Sorting cars by their distance from target simplifies the fleet formation logic
  • Catch-up condition: The key insight is that if a car’s arrival time ≤ previous car’s arrival time, they form a fleet
  • Greedy nature: This problem demonstrates a greedy approach where we process cars in order and make optimal decisions at each step

Encountered Problems
#

Related

LeetCode 739: Daily Temperatures
·2 mins
LeetCode Daily Medium Array Stack Monotonic-Stack Data-Structures Temperature Waiting-Time
Solving the Daily Temperatures problem using monotonic stack approach
LeetCode 503: Next Greater Element II (Monotonic Stack)
·2 mins
LeetCode Daily Medium Array Stack Monotonic-Stack Circular-Array
Monotonic stack on a circular array
LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them
LeetCode 2410: Maximum Matching of Players With Trainers
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 2410 note: sort both sides, then same-direction pointers match the weakest player with the smallest trainer that is good enough
LeetCode 735: Asteroid Collision
·2 mins
LeetCode Medium Array Stack Simulation
LeetCode 735 note: scan left to right with a stack; only a right-moving top meeting a left-moving asteroid collides, the smaller explodes and equal sizes both do
LeetCode 3075: Maximize Happiness of Selected Children
·1 min
LeetCode Medium Array Greedy Sorting
LeetCode 3075 note: choose k children from the happiest, where the i-th choice loses i, floored at 0