{"id":8176,"date":"2021-02-27T23:45:38","date_gmt":"2021-02-28T07:45:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8176"},"modified":"2021-02-27T23:46:40","modified_gmt":"2021-02-28T07:46:40","slug":"leetcode-1776-car-fleet-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-1776-car-fleet-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1776. Car Fleet II"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;cars traveling at different speeds in the same direction along a one-lane road. You are given an array&nbsp;<code>cars<\/code>&nbsp;of length&nbsp;<code>n<\/code>, where&nbsp;<code>cars[i] = [position<sub>i<\/sub>, speed<sub>i<\/sub>]<\/code>&nbsp;represents:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>position<sub>i<\/sub><\/code>&nbsp;is the distance between the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;car and the beginning of the road in meters. It is guaranteed that&nbsp;<code>position<sub>i<\/sub>&nbsp;&lt; position<sub>i+1<\/sub><\/code>.<\/li><li><code>speed<sub>i<\/sub><\/code>&nbsp;is the initial speed of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;car in meters per second.<\/li><\/ul>\n\n\n\n<p>For simplicity, cars can be considered as points moving along the number line. Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the&nbsp;<strong>slowest<\/strong>&nbsp;car in the fleet.<\/p>\n\n\n\n<p>Return an array&nbsp;<code>answer<\/code>, where&nbsp;<code>answer[i]<\/code>&nbsp;is the time, in seconds, at which the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;car collides with the next car, or&nbsp;<code>-1<\/code>&nbsp;if the car does not collide with the next car. Answers within&nbsp;<code>10<sup>-5<\/sup><\/code>&nbsp;of the actual answers are accepted.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cars = [[1,2],[2,1],[4,3],[7,2]]\n<strong>Output:<\/strong> [1.00000,-1.00000,3.00000,-1.00000]\n<strong>Explanation:<\/strong> After exactly one second, the first car will collide with the second car, and form a car fleet with speed 1 m\/s. After exactly 3 seconds, the third car will collide with the fourth car, and form a car fleet with speed 2 m\/s.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cars = [[3,4],[5,4],[6,3],[9,1]]\n<strong>Output:<\/strong> [2.00000,1.00000,1.50000,-1.00000]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= cars.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= position<sub>i<\/sub>, speed<sub>i<\/sub>&nbsp;&lt;= 10<sup>6<\/sup><\/code><\/li><li><code>position<sub>i<\/sub>&nbsp;&lt; position<sub>i+1<\/sub><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Monotonic Stack<\/strong><\/h2>\n\n\n\n<p>Key observation: If my speed is slower than the speed of the previous car, not only mine but also all cars behind me will NEVER be able to catch\/collide with the previous car. Such that we can throw it away.<\/p>\n\n\n\n<p>Maintain a stack that stores the indices of cars with increasing speed.<\/p>\n\n\n\n<p>Process car from right to left, for each car, pop the stack (throw the fastest car away) if any of the following conditions hold.<br>1)  speed &lt;= stack.top().speed<br>2) There are more than one car before me and it takes more than to collide the fastest car than time the fastest took to collide.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<double> getCollisionTimes(vector<vector<int>>& cars) {\n    auto collide = [&](int i, int j) -> double {\n      return static_cast<double>(cars[i][0] - cars[j][0]) \/\n        (cars[j][1] - cars[i][1]);\n    };\n    const int n = cars.size();\n    vector<double> ans(n, -1);\n    stack<int> s;\n    for (int i = n - 1; i >= 0; --i) {\n      while (!s.empty() && (cars[i][1] <= cars[s.top()][1] ||\n                           (s.size() > 1 && collide(i, s.top()) > ans[s.top()])))\n        s.pop();\n      ans[i] = s.empty() ? -1 : collide(i, s.top());\n      s.push(i);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;cars traveling at different speeds in the same direction along a one-lane road. You are given an array&nbsp;cars&nbsp;of length&nbsp;n, where&nbsp;cars[i] = [positioni, speedi]&nbsp;represents: positioni&nbsp;is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[217,552,180],"class_list":["post-8176","post","type-post","status-publish","format-standard","hentry","category-stack","tag-hard","tag-monotonic-stack","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8176"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8176\/revisions"}],"predecessor-version":[{"id":8178,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8176\/revisions\/8178"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}