`N` cars are going to the same destination along a one lane road.  The destination is `target` miles away.

Each car i has a constant speed speed[i] (in miles per hour), and initial position position[i] miles towards the target along the road.

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 distance between these two cars is ignored - they are assumed to have the same position.

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.

How many car fleets 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 and 3 become a fleet, meeting each other at 6.
Note that no other cars meet these fleets before the destination, so the answer is 3.\
Note:
  1. 0 <= N <= 10 ^ 4
  2. 0 < target <= 10 ^ 6
  3. 0 < speed[i] <= 10 ^ 6
  4. 0 <= position[i] < target
  5. All initial positions are different.

这道题说是路上有一系列的车,车在不同的位置,且分别有着不同的速度,但行驶的方向都相同。如果后方的车在到达终点之前追上前面的车了,那么它就会如痴汉般尾随在其后,且速度降至和前面的车相同,可以看作是一个车队,当然,单独的一辆车也可以看作是一个车队,问我们共有多少个车队到达终点。这道题是小学时候的应用题的感觉,什么狗追人啊,人追狗啊之类的。这道题的正确解法的思路其实不太容易想,因为我们很容易把注意力都集中到每辆车,去计算其每个时刻所在的位置,以及跟前面的车相遇的位置,这其实把这道题想复杂了,其实并不需要知道车的相遇位置,只关心是否能组成车队一同经过终点线,那么如何才能知道是否能一起过线呢,最简单的方法就是看时间,假如车B在车A的后面,而车B到终点线的时间小于等于车A,那么就知道车A和B一定会组成车队一起过线。这样的话,就可以从离终点最近的一辆车开始,先算出其撞线的时间,然后再一次遍历身后的车,若后面的车撞线的时间小于等于前面的车的时间,则会组成车队。反之,若大于前面的车的时间,则说明无法追上前面的车,于是自己会形成一个新的车队,且是车头,则结果 res 自增1即可。

思路有了,就可以具体实现了,使用一个 TreeMap 来建立小车位置和其到达终点时间之间的映射,这里的时间使用 double 型,通过终点位置减去当前位置,并除以速度来获得。我们希望能从 position 大的小车开始处理,而 TreeMap 是把小的数字排在前面,这里使用了个小 trick,就是映射的时候使用的是 position 的负数,这样就能先处理原来 position 大的车,从而统计出正确的车队数量,参见代码如下:

解法一:

class Solution {
public:
int carFleet(int target, vector<int>& position, vector<int>& speed) {
int res = 0; double cur = 0;
map<int, double> pos2time;
for (int i = 0; i < position.size(); ++i) {
pos2time[-position[i]] = (double)(target - position[i]) / speed[i];
}
for (auto a : pos2time) {
if (a.second <= cur) continue;
cur = a.second;
++res;
}
return res;
}
};

我们也可以使用优先队列来做,由于其是按照从大到小的顺序自动排列的,所以不用使用上面的小 trick。还有一点和上面的不同的是,并没有在开始就计算过线时间,而是直接存的是速度,因为存整型肯定比存 double 型的要节省空间。在之后处理的时候,再取出位置和速度计算时间,然后再进行跟上面相同的操作即可,参见代码如下:


解法二:

class Solution {
public:
int carFleet(int target, vector<int>& position, vector<int>& speed) {
int res = 0; double cur = 0;
priority_queue<pair<int, int>> q;
for (int i = 0; i < position.size(); ++i) {
q.push({position[i], speed[i]});
}
while (!q.empty()) {
auto t = q.top(); q.pop();
double timeNeeded = (double)(target - t.first) / t.second;
if (timeNeeded <= cur) continue;
cur = timeNeeded;
++res;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/853

参考资料:

https://leetcode.com/problems/car-fleet/

https://leetcode.com/problems/car-fleet/discuss/180287/Java-Priority-Queue-Explained

https://leetcode.com/problems/car-fleet/discuss/139850/C%2B%2BJavaPython-Straight-Forward

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] Car Fleet 车队的更多相关文章

  1. [LeetCode] 853. Car Fleet 车队

    N cars are going to the same destination along a one lane road.  The destination is target miles awa ...

  2. odoo 12企业版与免费社区版的区别,价格策略与技术支持指南的全面解析

    Odoo / Ps Cloud收费企业版是对社区版的极大增强,除了增加了很多功能外,最大的功能区别是企业版支持条码而社区版不支持,企业版对手机支持更好.有单独的APP,最重要区别的是企业版提供底层技术 ...

  3. [Swift]LeetCode853. 车队 | Car Fleet

    N cars are going to the same destination along a one lane road.  The destination is target miles awa ...

  4. 【LeetCode】853. Car Fleet 解题报告(Python)

    [LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  5. LeetCode——853.车队

    N 辆车沿着一条车道驶向位于 target 英里之外的共同目的地. 每辆车 i 以恒定的速度 speed[i] (英里/小时),从初始位置 position[i] (英里) 沿车道驶向目的地. 一辆车 ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. 38.Odoo产品分析 (四) – 工具板块(7) – 车队管理(2)

    查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (四) – 工具板块(7) – 车队管理(1) 4 显示及状态说明 合同默认以列表视图显示:  当合约到期时,以红色显示,并显示状态为待关闭 ...

  8. LeetCode刷题总结-排序、并查集和图篇

    本文介绍LeetCode上有关排序.并查集和图的算法题,推荐刷题总数为15道.具体考点分析如下图: 一.排序 1.数组问题 题号:164. 最大间距,难度困难 题号:324. 摆动排序 II,难度中等 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. 简单的C#网络爬虫

    Source Code: http://download.csdn.net/download/qdalong/10271880 这是爬取网页内容,像是这对大家来说都是不难得,但是在这里有一些小改动,代 ...

  2. 大前端服务器渲染 发布和部署 Vue + vue(SSR)

    https://blog.csdn.net/sinat_15951543/article/details/80109521    就是到服务器dist 下面 npm run start & 然 ...

  3. Linux系统(虚拟机)安装禅道

    1.查看linux系统版本 uname -a 2.禅道下载:http://www.zentao.net/download.html,找到要下载的版本,点击进入各平台下载: 3.将下载好的安装包上传到l ...

  4. JS中定义对象和集合

    在js中定义对象: 方式一: var obj = {}; obj['a']=1; obj['b']=2; 方式二: var obj=new Object(); obj.a=1; obj.b=2; 在j ...

  5. TP-Shop安装步骤教程(Windows版)

    TP-Shop安装步骤教程(Windows版) PS:首次发文,请多指教! 一.安装要求 1.PHP5.4以上,MYsql5.5以上. 2.需要Phpcurl,gd库.php_mysqli,php_o ...

  6. RabbitMQ通过Exchange.fanout、不同的队列绑定同一个Exchange实现多播处理

    消费者1: static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory() { HostNa ...

  7. arrayList和vector的区别--2019-4-16

    1. Vector & ArrayList 1)  Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的 ...

  8. Unity Shader Learning

    Toon 表面没有均匀的阴影. 为了达到这个效果,我们需要一个斜坡图. 其目的是将朗伯光强度NdotL重新映射到另一个值. 使用没有渐变的渐变映射,我们可以强制照明逐步渲染.下图显示了如何使用斜坡图来 ...

  9. Codeforces 938D. Buy a Ticket (最短路+建图)

    <题目链接> 题目大意: 有n座城市,每一个城市都有一个听演唱会的价格,这n座城市由m条无向边连接,每天变都有其对应的边权.现在要求出每个城市的人,看一场演唱会的最小价值(总共花费的价值= ...

  10. Ubuntu宿主机与VMware中其他系统虚拟机的互通

    Ubuntu做宿主机,VMware中创建Windows10,并且通过三种模式实现两系统互通,其实并非是件难事.在有线网卡未接网线的环境下,关闭两系统防火墙,基本遵从下文便可实现. 转载:https:/ ...