NeetCode #357LC-853MediumStackNC 150NC 250
← Back to All Problems#357 · #853 · Car Fleet(车队)
📌 Problem Statement & Constraints
There are
n cars on a one-lane road heading to a target mile destination. position[i] and speed[i] describe car i. A faster car behind a slower one cannot pass, so it joins that car's fleet. A fleet is a group of cars at the same position and speed. Return the number of fleets that arrive at the target. Constraints: 1 <= n <= 10^5, 0 < target <= 10^6, all positions are distinct.💡 Core Algorithmic Approaches
- Sort the cars by position in descending order, so the closest car to the target is processed first.
- Compute each car's arrival time as
(target - position) / speed. - A car forms a new fleet if its arrival time is strictly greater than the fleet ahead of it; otherwise it catches up and joins.
- Track only the last fleet's time, since any car that joins a fleet is governed by that fleet's leader.
💻 Benchmark Python3 Implementation
class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
cars = sorted(zip(position, speed), reverse=True) # closest first
fleets = 0
lead_d = lead_s = 0 # the fleet leader's (distance, speed)
for p, s in cars:
d = target - p
# new fleet if this car arrives strictly later than the leader
# compare d/s > lead_d/lead_s via cross multiplication (no floats)
if fleets == 0 or d * lead_s > lead_d * s:
fleets += 1
lead_d, lead_s = d, s
return fleets⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n log n): the sort dominates; the scan is O(n).
💾 Space Complexity
O(n) for the sorted pairs (O(1) extra beyond that).
⚠️ Interview Pitfalls & Follow-ups
- Sorting ascending: the closest car must be processed first, so the order is descending by position.
- Using
>=in the comparison: an equal arrival time means the car catches the fleet and joins it, so the test is strictly greater. - Comparing floats:
(target - p) / scan produce precision issues; the cross-multiplied integer comparison is exact. - Thinking a faster car behind always forms its own fleet: it only does so if it arrives later than the fleet ahead.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Stack Problems
中文速览与面试要点
车队
在一条单行道上,有 n 辆车正在往同一目的地行驶。目的地距起点 target 英里。给你两个整数数组 position 和 speed,长度都是 n,其中 position[i] 是第 i 辆车的位置, speed[i] 是第 i 辆车的速度。一辆车永远不能超过前面的另一辆车,但它可以追上去,并以较慢车的速度行驶。此时,我们忽略这两辆车之间的距离,也就是说,它们假定在同一位置。车队是一些由行驶在相同位置和相同速度的汽车组成的非空集合。注意,如果一辆车在目的地追上前面的一辆车,它们仍被认为是一个车队。返回到达目的地的 车队数量 。约束:n == position.length == speed.length;1 ≤ n ≤ 10^5;0 < target ≤ 10^6;0 ≤ position[i] < target;0 < speed[i] ≤ 10^6;position 中的所有值都 不同 。
核心要点
- 按位置降序排序 + 单调栈思想(标准解) :把车按位置从大到小排序(从最靠近终点的车开始处理)。
- 计算到达时间 : t = (target − position) / speed 。
- 判断是否形成车队 :维护当前「车队领头车」的到达时间 cur_time 。若当前车的到达时间 t > cur_time ,说明它追不上前面的车,会形成 新的车队 (车队数加一,并更新 cur_time = t);否则它会并入前一个车队。
- 为什么从后往前处理 :车不能超车,因此后面的车只能被前面的车「限制」。从最前面的车开始处理,可以依次确定每个车队的领头车。
常见陷阱
- 按位置升序处理 :方向反了。必须从最靠近终点的车开始(降序)。
- 判断条件写成 t >= cur_time :相等时应并入同一车队(同时到达),只有严格大于才形成新车队。
- 用浮点比较担心精度 : (target − p) / s 是精确的浮点除法,本题数据规模下精度足够;若担心可用交叉相乘比较,但没必要。
- 误以为要模拟行驶过程 :只需比较到达时间,无需逐步模拟。
参考实现
class Solution:
def carFleet(self, target: int, position: list[int], speed: list[int]) -> int:
# 按位置降序(最靠近终点的车排在最前)
cars = sorted(zip(position, speed), reverse=True)
fleets = 0
cur_time = 0.0 # 当前车队的领头车到达时间
for p, s in cars:
t = (target - p) / s
if t > cur_time: # 追不上前面的车 -> 形成新车队
fleets += 1
cur_time = t
return fleets