Skip to main content
LeetCode 853, Medium. Topics: Array, Stack, Sorting, Monotonic Stack. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 13 parametrized pytest cases, and a playground notebook:

Problem

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<sup>th</sup> car and speed[i] is the speed of the i<sup>th</sup> 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 is the minimum speed of any car in the fleet. If a car catches up to a car fleet at the mile target, it will still be considered as part of the car fleet. Return the number of car fleets that will arrive at the destination.

Examples

Constraints

  • n == position.length == speed.length
  • 1 <= n <= 10<sup>5</sup>
  • 0 < target <= 10<sup>6</sup>
  • 0 <= position[i] < target
  • All the values of position are unique.
  • 0 < speed[i] <= 10<sup>6</sup>

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 150, NeetCode 250, NeetCode All.
Last modified on August 25, 2026