ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 853. Car Fleet

2022-08-01 15:35:02  阅读:205  来源: 互联网

标签:target Car cars car position speed LeetCode Fleet 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 len = position.length;
 4         double[][] cars = new double[len][2];
 5         for (int i = 0; i < len; i++) {
 6             cars[i] = new double[] { position[i], (double) (target - position[i]) / speed[i] };
 7         }
 8         Arrays.sort(cars, (a, b) -> Double.compare(a[0], b[0]));
 9         double minTime = 0;
10         int res = 0;
11         for (int i = len - 1; i >= 0; i--) {
12             if (cars[i][1] > minTime) {
13                 res++;
14                 minTime = cars[i][1];
15             }
16         }
17         return res;
18     }
19 }

 

LeetCode 题目总结

标签:target,Car,cars,car,position,speed,LeetCode,Fleet,fleet
来源: https://www.cnblogs.com/cnoodle/p/16540456.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有