英文原文
There are n
cars going to the same destination along a one-lane road. The destination is target
miles away.
You are given two integer array position
and speed
, both of length n
, where position[i]
is the position of the ith
car and speed[i]
is the speed of the ith
car (in miles per hour).
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. The faster car will slow down to match the slower car's speed. The distance between these two cars is ignored (i.e., they are assumed to have the same position).
A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.
If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.
Return the number of car fleets that will arrive at the destination.
Example 1:
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] Output: 3 Explanation: The cars starting at 10 and 8 become a fleet, meeting each other at 12. The car starting at 0 doesn't catch up to any other car, so it is a fleet by itself. The cars starting at 5 (speed 3) and 3 (speed 1) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target. Note that no other cars meet these fleets before the destination, so the answer is 3.
Example 2:
Input: target = 10, position = [3], speed = [3] Output: 1 Explanation: There is only one car, hence there is only one fleet.
Example 3:
Input: target = 100, position = [0,2,4], speed = [4,2,1] Output: 1 Explanation: 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. 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.
Constraints:
n == position.length == speed.length
1 <= n <= 105
0 < target <= 106
0 <= position[i] < target
- All the values of
position
are unique. 0 < speed[i] <= 106
中文题目
N
辆车沿着一条车道驶向位于 target
英里之外的共同目的地。
每辆车 i
以恒定的速度 speed[i]
(英里/小时),从初始位置 position[i]
(英里) 沿车道驶向目的地。
一辆车永远不会超过前面的另一辆车,但它可以追上去,并与前车以相同的速度紧接着行驶。
此时,我们会忽略这两辆车之间的距离,也就是说,它们被假定处于相同的位置。
车队 是一些由行驶在相同位置、具有相同速度的车组成的非空集合。注意,一辆车也可以是一个车队。
即便一辆车在目的地才赶上了一个车队,它们仍然会被视作是同一个车队。
会有多少车队到达目的地?
示例:
输入:target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] 输出:3 解释: 从 10 和 8 开始的车会组成一个车队,它们在 12 处相遇。 从 0 处开始的车无法追上其它车,所以它自己就是一个车队。 从 5 和 3 开始的车会组成一个车队,它们在 6 处相遇。 请注意,在到达目的地之前没有其它车会遇到这些车队,所以答案是 3。
提示:
0 <= N <= 10 ^ 4
0 < target <= 10 ^ 6
0 < speed[i] <= 10 ^ 6
0 <= position[i] < target
- 所有车的初始位置各不相同。
通过代码
官方题解
方法一:排序
分析
我们首先对这些车辆按照它们的起始位置降序排序,并且用 (target - position) / speed
计算出每辆车在不受其余车的影响时,行驶到终点需要的时间。对于相邻的两辆车 S
和 F
,F
的起始位置大于 S
,如果 S
行驶到终点需要的时间小于等于 F
,那么 S
一定会在终点前追上 F
并形成车队。这是因为在追上 F
之前,S
的行驶速度并不会减小,而 F
却有可能因为追上前面的车辆而速度减小,因此 S
总能在终点前追上 F
。
算法
将车辆按照起始位置降序排序后,我们顺序扫描这些车辆。如果相邻的两辆车,前者比后者行驶到终点需要的时间短,那么后者永远追不上前者,即从后者开始的若干辆车辆会组成一个新的车队;如果前者不比后者行驶到终点需要的时间短,那么后者可以在终点前追上前者,并和前者形成车队。此时我们将后者到达终点的时间置为前者到达终点的时间。
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
int N = position.length;
Car[] cars = new Car[N];
for (int i = 0; i < N; ++i)
cars[i] = new Car(position[i], (double) (target - position[i]) / speed[i]);
Arrays.sort(cars, (a, b) -> Integer.compare(a.position, b.position));
int ans = 0, t = N;
while (--t > 0) {
if (cars[t].time < cars[t-1].time) ans++; //if cars[t] arrives sooner, it can't be caught
else cars[t-1] = cars[t]; //else, cars[t-1] arrives at same time as cars[t]
}
return ans + (t == 0 ? 1 : 0); //lone car is fleet (if it exists)
}
}
class Car {
int position;
double time;
Car(int p, double t) {
position = p;
time = t;
}
}
class Solution(object):
def carFleet(self, target, position, speed):
cars = sorted(zip(position, speed))
times = [float(target - p) / s for p, s in cars]
ans = 0
while len(times) > 1:
lead = times.pop()
if lead < times[-1]: ans += 1 # if lead arrives sooner, it can't be caught
else: times[-1] = lead # else, fleet arrives at later time 'lead'
return ans + bool(times) # remaining car is fleet (if it exists)
复杂度分析
时间复杂度:$O(N \log N)$,即为排序的时间复杂度。
空间复杂度:$O(N)$,存储车辆到达终点需要的时间。
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
10852 | 28089 | 38.6% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|