Skip to main content
  1. LeetCode/

LeetCode 853: Car Fleet

·2 mins· ·
LeetCode Daily Medium Stack Sorting Simulation Greedy Array
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 Stack Monotonic-Stack Array Temperature Waiting-Time Algorithm Data-Structure
Solving the Daily Temperatures problem using monotonic stack approach
LeetCode 503: Next Greater Element II (Monotonic Stack)
·2 mins
LeetCode Daily Medium Monotonic-Stack Stack Array Circular-Array
Monotonic stack on a circular array
LeetCode 150: Evaluate Reverse Polish Notation
·2 mins
LeetCode Daily Medium Stack Expression-Evaluation Reverse-Polish-Notation Postfix-Notation Mathematics Algorithm Data-Structure
Solving the Reverse Polish Notation evaluation problem using stack-based approach
LeetCode 84: Largest Rectangle in Histogram
·3 mins
LeetCode Daily Hard Stack Monotonic-Stack Array Histogram Geometry Algorithm Dynamic-Programming
Solving the Largest Rectangle in Histogram problem using monotonic stack approach
LeetCode 901: Online Stock Span
·2 mins
LeetCode Daily Medium Stack Monotonic-Stack Design Data-Structure Algorithm
Solving the Online Stock Span problem using monotonic stack approach
LeetCode 1497: Check If Array Pairs Are Divisible by k (Remainder Pairing, Modulo)
·2 mins
LeetCode Daily Medium Array Hash Map Modulo Math Counting Complement
Remainder pairing with a hash map; handle 0 and k/2 remainders carefully.