Skip to content

Commit 0292296

Browse files
committed
car fleet
1 parent 8306376 commit 0292296

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

853.CarFleet.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
853. Car Fleet
3+
Medium
4+
Array | Stack | Sorting | Monotonic Stack
5+
---
6+
There are n cars going to the same destination along a one-lane road. The destination is target miles away.
7+
8+
You are given two integer array position and speed, both of length n,
9+
where position[i] is the position of the ith car and speed[i] is the speed of the ith car (in miles per hour).
10+
11+
A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper at the same speed.
12+
The faster car will slow down to match the slower car's speed.
13+
The distance between these two cars is ignored (i.e., they are assumed to have the same position).
14+
15+
A car fleet is some non-empty set of cars driving at the same position and same speed.
16+
Note that a single car is also a car fleet.
17+
18+
If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.
19+
20+
Return the number of car fleets that will arrive at the destination.
21+
22+
Example 1:
23+
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
24+
Output: 3
25+
Explanation:
26+
The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12.
27+
The car starting at 0 does not catch up to any other car, so it is a fleet by itself.
28+
The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
29+
Note that no other cars meet these fleets before the destination, so the answer is 3.
30+
31+
Example 2:
32+
Input: target = 10, position = [3], speed = [3]
33+
Output: 1
34+
Explanation: There is only one car, hence there is only one fleet.
35+
36+
Example 3:
37+
Input: target = 100, position = [0,2,4], speed = [4,2,1]
38+
Output: 1
39+
Explanation:
40+
The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The fleet moves at speed 2.
41+
Then, the fleet (speed 2) and the car starting at 4 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
42+
43+
Constraints:
44+
n == position.length == speed.length
45+
1 <= n <= 105
46+
0 < target <= 106
47+
0 <= position[i] < target
48+
All the values of position are unique.
49+
0 < speed[i] <= 106
50+
"""
51+
52+
from typing import List
53+
54+
55+
# O(n log n) time | O(n) space
56+
class Solution:
57+
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
58+
stack = []
59+
60+
for pos, spd in sorted(zip(position, speed), reverse=True):
61+
time = (target - pos) / spd
62+
if not stack:
63+
stack.append(time)
64+
elif time > stack[-1]:
65+
stack.append(time)
66+
67+
return len(stack)

0 commit comments

Comments
 (0)