{"id":5241,"date":"2019-06-22T21:32:35","date_gmt":"2019-06-23T04:32:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5241"},"modified":"2019-06-26T08:06:41","modified_gmt":"2019-06-26T15:06:41","slug":"leetcode-1094-car-pooling","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/heap\/leetcode-1094-car-pooling\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1094. Car Pooling"},"content":{"rendered":"\n<p>You are driving a vehicle that&nbsp;has&nbsp;<code>capacity<\/code>&nbsp;empty seats initially available for passengers.&nbsp; The vehicle&nbsp;<strong>only<\/strong>&nbsp;drives east (ie. it&nbsp;<strong>cannot<\/strong>&nbsp;turn around and drive west.)<\/p>\n\n\n\n<p>Given a list of&nbsp;<code>trips<\/code>,&nbsp;<code>trip[i] = [num_passengers, start_location, end_location]<\/code>&nbsp;contains information about the&nbsp;<code>i<\/code>-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off.&nbsp; The locations are given as the number of kilometers&nbsp;due east from your vehicle&#8217;s initial location.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;if and only if&nbsp;it is possible to pick up and drop off all passengers for all the given trips.&nbsp;<\/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>trips = [[2,1,5],[3,3,7]], capacity = 4\n<strong>Output: <\/strong>false\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>trips = [[2,1,5],[3,3,7]], capacity = 5\n<strong>Output: <\/strong>true\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>trips = [[2,1,5],[3,5,7]], capacity = 3\n<strong>Output: <\/strong>true\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11\n<strong>Output: <\/strong>true<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution1: Min heap<\/strong><\/h2>\n\n\n\n<p>Sort events by location<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<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  bool carPooling(vector<vector<int>>& trips, int capacity) {\n    priority_queue<int> q;\n    for (const auto& trip : trips) {\n      int pick_up = -((trip[1] << 10) | (1 << 9) | trip[0]);\n      int drop_off = -((trip[2] << 10) | trip[0]);\n      q.push(pick_up);\n      q.push(drop_off);\n    }\n    while (q.size()) {\n      int key = -q.top(); q.pop();\n      int sign = ((key >> 9) & 1) ? 1: -1;\n      int num = key & 0xFF;\n      if ((capacity -= sign * num) < 0)\n        return false;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Preprocessing<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1000)<\/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  bool carPooling(vector<vector<int>>& trips, int capacity) {\n    vector<int> d(1001);\n    for (const auto& trip : trips) {\n      d[trip[1]] -= trip[0];\n      d[trip[2]] += trip[0];\n    }\n    for (const int c : d) \n      if ((capacity += c) < 0) return false;\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are driving a vehicle that&nbsp;has&nbsp;capacity&nbsp;empty seats initially available for passengers.&nbsp; The vehicle&nbsp;only&nbsp;drives east (ie. it&nbsp;cannot&nbsp;turn around and drive west.) Given a list of&nbsp;trips,&nbsp;trip[i] =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[73,177],"class_list":["post-5241","post","type-post","status-publish","format-standard","hentry","category-heap","tag-heap","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5241","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=5241"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5241\/revisions"}],"predecessor-version":[{"id":5260,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5241\/revisions\/5260"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}