N
cars are going to the same destination along a one lane road. The destination is target
miles away.html
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.git
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.github
The distance between these two cars is ignored - they are assumed to have the same position.ide
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.code
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?htm
Example 1:blog
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:
0 <= N <= 10 ^ 4
0 < target <= 10 ^ 6
0 < speed[i] <= 10 ^ 6
0 <= position[i] < target
这道题说是路上有一系列的车,车在不一样的位置,且分别有着不一样的速度,但行驶的方向都相同。若是后方的车在到达终点以前追上前面的车了,那么它就会如痴汉般尾随在其后,且速度降至和前面的车相同,能够看做是一个车队,固然,单独的一辆车也能够看做是一个车队,问咱们共有多少个车队到达终点。这道题是小学时候的应用题的感受,什么狗追人啊,人追狗啊之类的。这道题的正确解法的思路其实不太容易想,由于咱们很容易把注意力都集中到每辆车,去计算其每一个时刻所在的位置,以及跟前面的车相遇的位置,这其实把这道题想复杂了,其实并不须要知道车的相遇位置,只关心是否能组成车队一同通过终点线,那么如何才能知道是否能一块儿过线呢,最简单的方法就是看时间,假如车B在车A的后面,而车B到终点线的时间小于等于车A,那么就知道车A和B必定会组成车队一块儿过线。这样的话,就能够从离终点最近的一辆车开始,先算出其撞线的时间,而后再一次遍历身后的车,若后面的车撞线的时间小于等于前面的车的时间,则会组成车队。反之,若大于前面的车的时间,则说明没法追上前面的车,因而本身会造成一个新的车队,且是车头,则结果 res 自增1便可。队列
思路有了,就能够具体实现了,使用一个 TreeMap 来创建小车位置和其到达终点时间之间的映射,这里的时间使用 double 型,经过终点位置减去当前位置,并除以速度来得到。咱们但愿能从 position 大的小车开始处理,而 TreeMap 是把小的数字排在前面,这里使用了个小 trick,就是映射的时候使用的是 position 的负数,这样就能先处理原来 position 大的车,从而统计出正确的车队数量,参见代码以下:leetcode
解法一:get
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