853. Car Fleet

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

https://leetcode.com/problems/car-fleet/discuss/139999/Easy-understanding-JAVA-TreeMap-Solution-with-explanation-and-comment

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.
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.
?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.

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.

If one car catch up the one before it, it means the time it takes to reach the target is shorter than the one in front(if no blocking).?For example:?car A at pos a with speed sa?car B at pos b with speed sb?(b < a < target)?Their distances to the target are (target-a) and (target-b).?If (target-a)/sa > (target-b)/sb it means car A take more time to reach target so car B will catch up car A and form a single group.

So we use the distance to target as key and speed as value, iterate through all cars in order of their distances to the target.?keep track of currently slowest one(which might block the car behind), if a car can catch up current slowest one, it will not cause a new group.?Otherwise, we count a new group and update the info of slowest

class Solution {
    public int carFleet(int target, int[] position, int[] speed) {
        TreeMap<Integer, Double> map = new TreeMap<>();
        for(int i = 0; i < position.length; i++){
            // key : - distance, value : time takes to the target
            // so the map is ordered by the cloest distance to target
            // - 10 , -5, -2
            map.put(-position[i], (double)(target - position[i]) / speed[i]);
        }
        int res = 0;
        double cur = 0; // the car ahead of it takes cur time to target
        // pos : a, b .. if a takes more time to reach the target than b does, then a new group is started
        // and update the new time for this group
        for(double time : map.values()){
            if(time > cur){
                cur = time;
                res++;
            }
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9929750.html

时间: 2024-10-14 22:06:57

853. Car Fleet的相关文章

[LeetCode] 853. Car Fleet 车队

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 n

LeetCode 853. Car Fleet

1AC - Yay! A super cute one : ) Again, simulate & play with it in your mind, and you will get it. Key: for car[i] at its position, it can only pass the target with the slowest car ahead of it. So the problem becomes, count # of `slowest` cars in the

如何利用fleet单元文件为CoreOS集群创建高灵活性服务

提供:ZStack云计算 系列教程 本教程为CoreOS上手指南系列九篇中的第六篇. 内容简介 CoreOS能够利用一系列工具以集群化与Docker容器化方式简化服务管理工作.其中etcd负责将各独立节点联系起来并提供全局数据平台,而大部分实际服务管理任务则由fleet守护进程实现. 在上一篇教程中,我们了解了如何利用fleetctl命令操纵服务及集群成员.在今天的教程中,我们将了解如何利用单元文件定义服务. 在接下来的内容中,我们将探讨如何构建fleet单元文件,外加在生产环境下提升服务健壮性

[CoreOS 转载] CoreOS实践指南(四):集群的指挥所Fleet

转载:http://www.csdn.net/article/2015-01-14/2823554/2 摘要:CoreOS是采用了高度精简的系统内核及外围定制的操作系统.ThoughtWorks的软件工程师林帆将带来“漫步云端:CoreOS实践指南”系列文章,介绍CoreOS精华和推荐的实践方法.本文为基础第四篇:集群的指挥所Fleet. 集群上的服务生命周期 刚刚的启动流程看起来很简单,不是么?在实际的使用中,如果为了省事,用Fleet启动一个服务,这样做就可以了.但这种做法其实会带来的服务管

FLEET 框架:研发时间减半质量倍增的秘密

近五年来, Agilean 一直在帮助某大型股份行实施精益看板转型.从全面导入到落地深化,从基层赋能到量化改进,该行目前电子看板使用率已达90%,为国内领先水平.在此过程中,我们将精益思想和看板方法进行了更有机的贯通,形成了一个帮助企业快速提升研发效能的改进体系 FLEET(精益效能提升思维框架,Framework of Lean Efficacy Enhancement Thinking).我将结合落地经验,对FLEET进行全方位阐释. 以下为FLEET 的的6条核心思路: 看见:高效知识工作

Fleet of the Eternal Throne HDU6138

不知道为什么今天晚上神经病,一直睡不着,挣扎了四个多小时,还是决定起来搞点东西,就补了一题:A了之后对比了一下标程似乎更优化~~~快了6倍多代码也很短~ 思路:对所有子串建立AC自动机,然后只需要定义两个数组,一个是每个节点的父亲,一个是每组字符串的最后一个字符的节点标号,然后就顺着每一个x串的fail指针去标记一下,然后顺着y串的fail去搜,搜到标记过的,就更新一下当前最大就好,然后就是结果了,复杂度O(n): 代码: #include <cstdio> #include <algo

2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 解法1:AC自动机.做法是把n个串建成AC自动机,前缀树中每个节点都当做结尾节点,val赋为trie树深度,然后把x串丢进自动机里,把匹配到的前缀节点染个色,再把y串丢进去,遇到同样颜色的前缀节点就更新一下答案. #include <bits/stdc++.h> using namespace s

HDU 6138 Fleet of the Eternal Throne(AC自动机)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6138 [题目大意] 给出一些串,询问第x个串和第y个串的公共子串, 同时要求该公共子串为某个串的前缀.求最长符合要求的答案 [题解] 我们对所有串构建AC自动机,将两个询问串之一在AC自动机上mark所有的匹配位置 另一个串在mark的地方寻找最长匹配即可 [代码] #include <cstdio> #include <algorithm> #include <cstrin

cf 853 A planning [贪心]

题面: 传送门 思路: 一眼看得,这是贪心[雾] 实际上,我们要求的答案就是sigma(ci*(ti-i))(i=1~n),这其中sigma(ci*i)是确定的 那么我们就要最小化sigma(ci*ti) 所以在新的每一秒,就把这一秒开始可以起飞的飞机中,cost最大的那一个拿出来,让他起飞就可以了 证明: 设最大的为m,我们取得另一个为n 那么n*ti+m*(ti+1) >= n*(ti+1)+m*ti 所以取m最好 这个过程用堆实现,懒得手打了,就用了priority_queue Code: