[LeetCode] 853. Car Fleet
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 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The car starting at 0 does not catch up to any other car, so it is a fleet by itself. 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. 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 。
给定两个整数数组 position 和 speed ,长度都是 n ,其中 position[i] 是第 i 辆车的位置, speed[i] 是第 i 辆车的速度(单位是英里/小时)。
一辆车永远不会超过前面的另一辆车,但它可以追上去,并与前车 以相同的速度 紧接着行驶。此时,我们会忽略这两辆车之间的距离,也就是说,它们被假定处于相同的位置。
车队 是一些由行驶在相同位置、具有相同速度的车组成的非空集合。注意,一辆车也可以是一个车队。
即便一辆车在目的地才赶上了一个车队,它们仍然会被视作是同一个车队。
返回到达目的地的 车队数量 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/car-fleet
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题看着像一个数组的题,但是做法不止一种,给的条件也不是很直接。我暂时提供其中的一种方法,大致思路是需要排序。
题意给的是几个数组,其中 position 数组表示的是所有的车辆一开始的位置信息(起点),target 表示终点,speed 数组记录的是每辆车的速度信息。有了起点和终点,我们就知道了每辆车各自需要行驶的距离 distance = target - position[i],又因为有了每辆车的速度信息,我们也就可以算出每辆车到达终点所需要的时间 time = distance / speed[i]。
此时我们把所有的车的信息按照他们需要行驶的距离 distance 排序,所以数组最左边的元素是距离终点最远的元素,数组最右边的元素是距离终点最近的元素。但是注意,越往数组右边的元素(距离终点越近的元素),他到达终点所花的时间 time 不一定是最近的。此时我们从右往左扫描数组,同时我们记录一个全局最小的时间 minTime,如果当前车辆需要的时间 time > minTime,说明他无法在到达终点前追上任何一部车,他就需要自成一个车队,res++,同时我们更新 minTime;反之如果当前的车需要的时间 time < minTime,就说明当前车在到达终点前可以追上之前的某个车队并加入他们。
时间O(nlogn)
空间O(n)
Java实现
1 class Solution { 2 public int carFleet(int target, int[] position, int[] speed) { 3 int n = position.length; 4 if (n <= 1) { 5 return n; 6 } 7 8 // (position, time to finish) 9 double[][] cars = new double[n][2]; 10 for (int i = 0; i < n; i++) { 11 cars[i][0] = position[i]; 12 cars[i][1] = (double) (target - position[i]) / speed[i]; 13 } 14 // 按离出发点距离从小到大排序 15 Arrays.sort(cars, (a, b) -> Double.compare(a[0], b[0])); 16 double cur = 0; 17 int res = 0; 18 for (int i = n - 1; i >= 0; i--) { 19 // 如果当前车的耗时更大,那么比他耗时更短的车他是追不上的,所以他只能自成一队 20 if (cars[i][1] > cur) { 21 cur = cars[i][1]; 22 res++; 23 } 24 } 25 return res; 26 } 27 }