跳转至

853. 车队

题目描述

在一条单行道上,有 n 辆车开往同一目的地。目的地是几英里以外的 target 。

给定两个整数数组 position 和 speed ,长度都是 n ,其中 position[i] 是第 i 辆车的位置, speed[i] 是第 i 辆车的速度(单位是英里/小时)。

一辆车永远不会超过前面的另一辆车,但它可以追上去,并以较慢车的速度在另一辆车旁边行驶。

车队 是指并排行驶的一辆或几辆汽车。车队的速度是车队中 最慢 的车的速度。

即便一辆车在 target 才赶上了一个车队,它们仍然会被视作是同一个车队。

返回到达目的地的车队数量 。

 

示例 1:

输入:target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]

输出:3

解释:

  • 从 10(速度为 2)和 8(速度为 4)开始的车会组成一个车队,它们在 12 相遇。车队在 target 形成。
  • 从 0(速度为 1)开始的车不会追上其它任何车,所以它自己是一个车队。
  • 从 5(速度为 1) 和 3(速度为 3)开始的车组成一个车队,在 6 相遇。车队以速度 1 移动直到它到达 target

示例 2:

输入:target = 10, position = [3], speed = [3]

输出:1

解释:

只有一辆车,因此只有一个车队。

示例 3:

输入:target = 100, position = [0,2,4], speed = [4,2,1]

输出:1

解释:

  • 从 0(速度为 4) 和 2(速度为 2)开始的车组成一个车队,在 4 相遇。从 4 开始的车(速度为 1)移动到了 5。
  • 然后,在 4(速度为 2)的车队和在 5(速度为 1)的车成为一个车队,在 6 相遇。车队以速度 1 移动直到它到达 target

 

提示:

  • n == position.length == speed.length
  • 1 <= n <= 105
  • 0 < target <= 106
  • 0 <= position[i] < target
  • position 中每个值都 不同
  • 0 < speed[i] <= 106

解法

方法一:排序

我们将车辆按照位置降序排序,这样我们只需要比较相邻两辆车的到达时间即可。

我们初始化一个变量 \(pre\) 表示上一辆车到达终点的时间,如果当前车辆到达终点的时间大于 \(pre\),说明当前车辆无法追上前面的车辆,因此需要另外开一个车队,否则当前车辆会与前面的车辆组成一个车队。

时间复杂度 \(O(n)\),空间复杂度 \(O(n)\)。其中 \(n\) 是车辆的数量。

 1  2  3  4  5  6  7  8  9 10
class Solution: def carFleet(self, target: int, position: List[int], speed: List[int]) -> int: idx = sorted(range(len(position)), key=lambda i: position[i]) ans = pre = 0 for i in idx[::-1]: t = (target - position[i]) / speed[i] if t > pre: ans += 1 pre = t return ans 
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
class Solution {  public int carFleet(int target, int[] position, int[] speed) {  int n = position.length;  Integer[] idx = new Integer[n];  Arrays.setAll(idx, i -> i);  Arrays.sort(idx, (i, j) -> position[j] - position[i]);  int ans = 0;  double pre = 0;  for (int i : idx) {  double t = 1.0 * (target - position[i]) / speed[i];  if (t > pre) {  ++ans;  pre = t;  }  }  return ans;  } } 
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
class Solution { public:  int carFleet(int target, vector<int>& position, vector<int>& speed) {  int n = position.size();  vector<int> idx(n);  iota(idx.begin(), idx.end(), 0);  sort(idx.begin(), idx.end(), [&](int i, int j) {  return position[i] > position[j];  });  int ans = 0;  double pre = 0;  for (int i : idx) {  double t = 1.0 * (target - position[i]) / speed[i];  if (t > pre) {  ++ans;  pre = t;  }  }  return ans;  } }; 
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
func carFleet(target int, position []int, speed []int) (ans int) {  n := len(position)  idx := make([]int, n)  for i := range idx {  idx[i] = i  }  sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] })  var pre float64  for _, i := range idx {  t := float64(target-position[i]) / float64(speed[i])  if t > pre {  ans++  pre = t  }  }  return } 
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
function carFleet(target: number, position: number[], speed: number[]): number {  const n = position.length;  const idx = Array(n)  .fill(0)  .map((_, i) => i)  .sort((i, j) => position[j] - position[i]);  let ans = 0;  let pre = 0;  for (const i of idx) {  const t = (target - position[i]) / speed[i];  if (t > pre) {  ++ans;  pre = t;  }  }  return ans; } 

评论