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.

N辆车沿着一条车道驶向位于target英里之外的共同目的地。每辆车i以恒定的速度speed[i](英里/小时),从初始位置 position[i](英里)沿车道驶向目的地。
一辆车永远不会超过前面的另一辆车,但它可以追上去,并与前车以相同的速度紧接着行驶。此时,我们会忽略这两辆车之间的距离,也就是说,它们被假定处于相同的位置。车队是一些由行驶在相同位置、具有相同速度的车组成的非空集合。注意,一辆车也可以是一个车队。即便一辆车在目的地才赶上了一个车队,它们仍然会被视作是同一个车队。求会有多少车队到达目的地?

解法:先把车按照位置进行排序,然后计算出每个车在无阻拦的情况下到达终点的时间,如果后面的车到达终点所用的时间比前面车小,那么说明后车会比前面的车先到,由于后车不能超过前车,所以这种情况下就会合并成一个车队。用栈来存,对时间进行遍历,对于那些应该合并的车不进栈就行了,最后返回栈的长度。或者直接用一个变量存最近前车到达时间,用另一变量记录车队的数量,如果循环的时间大于记录的前车时间,则当前的车不会比之前的车先到达,为一个新车队,更新变量。

Java:

public int carFleet(int target, int[] pos, int[] speed) {
TreeMap<Integer, Double> m = new TreeMap<>();
for (int i = 0; i < pos.length; ++i) m.put(-pos[i], (double)(target - pos[i]) / speed[i]);
int res = 0; double cur = 0;
for (double time : m.values()) {
if (time > cur) {
cur = time;
res++;
}
}
return res;
}

Java:

class Solution {
public int carFleet(int target, int[] position, int[] speed) {
int N = position.length;
int res = 0;
//建立位置-时间的N行2列的二维数组
double[][] cars = new double[N][2];
for (int i = 0; i < N; i++) {
cars[i] = new double[]{position[i], (double) (target - position[i]) / speed[i]};
}
Arrays.sort(cars, (a, b) -> Double.compare(a[0], b[0]));
double cur = 0; //从后往前比较
for (int i = N - 1; i >= 0; i--) {
if (cars[i][1] > cur) {
cur = cars[i][1];
res++;
}
}
return res;
} }  

Python:

 def carFleet(self, target, pos, speed):
time = [float(target - p) / s for p, s in sorted(zip(pos, speed))]
res = cur = 0
for t in time[::-1]:
if t > cur:
res += 1
cur = t
return res

Python:

class Solution:
def carFleet(self, target, position, speed):
"""
:type target: int
:type position: List[int]
:type speed: List[int]
:rtype: int
"""
cars = [(pos, spe) for pos, spe in zip(position, speed)]
sorted_cars = sorted(cars, reverse=True)
times = [(target - pos) / spe for pos, spe in sorted_cars]
stack = []
for time in times:
if not stack:
stack.append(time)
else:
if time > stack[-1]:
stack.append(time)
return len(stack)

Python:

# Time:  O(nlogn)
# Space: O(n)
class Solution(object):
def carFleet(self, target, position, speed):
"""
:type target: int
:type position: List[int]
:type speed: List[int]
:rtype: int
"""
times = [float(target-p)/s for p, s in sorted(zip(position, speed))]
result, curr = 0, 0
for t in reversed(times):
if t > curr:
result += 1
curr = t
return result 

C++:

int carFleet(int target, vector<int>& pos, vector<int>& speed) {
map<int, double> m;
for (int i = 0; i < pos.size(); i++) m[-pos[i]] = (double)(target - pos[i]) / speed[i];
int res = 0; double cur = 0;
for (auto it : m) if (it.second > cur) cur = it.second, res++;
return res;
}

  

  

  

All LeetCode Questions List 题目汇总

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

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

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

  2. [LeetCode] Car Fleet 车队

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

  3. LeetCode——853.车队

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

  4. All LeetCode Questions List 题目汇总

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

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

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

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

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

  7. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  8. Swift LeetCode 目录 | Catalog

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

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

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

随机推荐

  1. Django创建管理员账号

    python manage.py createsuperuser 创建一个管理员账号 输入账号:admin 输入邮箱:123456789@qq.com 输入密码:test123456 二次确认 pyt ...

  2. CentOS7卸载 OpenJDK 安装Sun的JDK8

    Linux上一般会安装Open JDK,关于OpenJDK和JDK的区别:http://www.cnblogs.com/sxdcgaq8080/p/7487369.html 下面开始安装步骤: --- ...

  3. C# CefSharp如何在Winforms应用程序中使用

    最近做了一个很小的功能,在网页上面打开应用程序,用vs的debug调试,可以正常打开应用程序,可布置到iis上面却无法运行应用程序,吾百度之,说是iis权限问题,吾依理做之,可怎么折腾也不行.最后bo ...

  4. 在inux中安装redis的时候,会出现下面的这个异常

    是因为没有安装c++的编译器 安装c++的编译器: yum -y install gcc-c++ 然后再使用命令执行make就可以了 ,如果你遇到这个错误以后,一定要先将redis的解压包删掉以后,再 ...

  5. 初学Django基础02 ORM操作

    django的ORM操作 之前我们知道了models.py这个文件,这个文件是用来读取数据结构的文件,每次操作数据时都走这个模块 常用字段 AutoField int自增列,必须填入参数 primar ...

  6. NodeJS 多版本管理(NVM)

    前言 现在前端各种框架更新较快,对 Node 的依赖也不一样,Node 的过版本管理也很有必要. NVM(Node Version Manager),是一个 Node 的版本管理工具. 官方的 NVM ...

  7. 关于input标签checkbox属性 和checked

    我们设置了type的属性为checkbox时,记住以下3个关键点 1.点勾选时或者说点击时,checked为选中,在input标签中是checked=“checked”,注意这里面无论checked= ...

  8. MapReduce如何解决数据倾斜?

    数据倾斜是日常大数据查询中隐形的一个BUG,遇不到它时你觉得数据倾斜也就是书本博客上的一个无病呻吟的偶然案例,但当你遇到它是你就会懊悔当初怎么不多了解一下这个赫赫有名的事故. https://www. ...

  9. L1025

    1,对于搜索,我有一个不成熟的想法,这不就是,强化版的for循环吗? 2,反正是搜索,那就先找搜索状态, n,x,n是第几次分,x是分剩下的数. 3,这个我觉得自己努力努力可能可以做出来. 4,首先要 ...

  10. 目标检测的mAp

    众多目标检测的知识中,都提到了mAp一值,那么这个东西到底是什么呢: 我们在评价一个目标检测算法的"好坏"程度的时候,往往采用的是pascal voc 2012的评价标准mAP.目 ...