{"id":3171,"date":"2018-07-14T23:10:52","date_gmt":"2018-07-15T06:10:52","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3171"},"modified":"2018-07-17T08:50:19","modified_gmt":"2018-07-17T15:50:19","slug":"leetcode-871-minimum-number-of-refueling-stops","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-871-minimum-number-of-refueling-stops\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 871. Minimum Number of Refueling Stops"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 871. Minimum Number of Refueling Stops - \u5237\u9898\u627e\u5de5\u4f5c EP207\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/vWTPA5zw24M?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>A car travels from a starting position to a destination which is\u00a0<code>target<\/code>\u00a0miles east of the starting position.<\/p>\n<p>Along the way, there are gas stations.\u00a0 Each\u00a0<code>station[i]<\/code>\u00a0represents a gas station that is\u00a0<code>station[i][0]<\/code>\u00a0miles east of the starting position, and has\u00a0<code>station[i][1]<\/code>\u00a0liters of gas.<\/p>\n<p>The car starts with an infinite tank of gas, which initially has\u00a0<code>startFuel<\/code>\u00a0liters of fuel in it.\u00a0 It uses 1 liter of gas per 1 mile that it drives.<\/p>\n<p>When the car\u00a0reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.<\/p>\n<p>What is the least number of refueling stops the car must make in order to reach its destination?\u00a0 If it cannot reach the destination, return\u00a0<code>-1<\/code>.<\/p>\n<p>Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.\u00a0 If the car reaches the destination with 0 fuel left, it is still considered to have arrived.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>target = <span id=\"example-input-1-1\">1<\/span>, startFuel = <span id=\"example-input-1-2\">1<\/span>, stations = <span id=\"example-input-1-3\">[]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">0<\/span>\r\n<strong>Explanation: <\/strong>We can reach the target without refueling.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>target = <span id=\"example-input-2-1\">100<\/span>, startFuel = <span id=\"example-input-2-2\">1<\/span>, stations = <span id=\"example-input-2-3\">[[10,100]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">-1<\/span>\r\n<strong>Explanation: <\/strong>We can't reach the target (or even the first gas station).\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>target = <span id=\"example-input-3-1\">100<\/span>, startFuel = <span id=\"example-input-3-2\">10<\/span>, stations = <span id=\"example-input-3-3\">[[10,60],[20,30],[30,30],[60,40]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">2<\/span>\r\n<strong>Explanation: <\/strong>\r\nWe start with 10 liters of fuel.\r\nWe drive to position 10, expending 10 liters of fuel.  We refuel from 0 liters to 60 liters of gas.\r\nThen, we drive from position 10 to position 60 (expending 50 liters of fuel),\r\nand refuel from 10 liters to 50 liters of gas.  We then drive to and reach the target.\r\nWe made 2 refueling stops along the way, so we return 2.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= target, startFuel, stations[i][1] &lt;= 10^9<\/code><\/li>\n<li><code>0 &lt;= stations.length &lt;= 500<\/code><\/li>\n<li><code>0 &lt; stations[0][0] &lt; stations[1][0] &lt; ... &lt; stations[stations.length-1][0] &lt; target<\/code><\/li>\n<\/ol>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3204\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/871-ep207.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/871-ep207.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/871-ep207-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/871-ep207-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution1: DP<\/strong><\/h1>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 24 ms\r\nclass Solution {\r\npublic:\r\n  int minRefuelStops(int target, int startFuel, vector&lt;vector&lt;int&gt;&gt;&amp; stations) {\r\n    const int n = stations.size();\r\n    \/\/ dp[i]: max distance to go with i stops.\r\n    vector&lt;long&gt; dp(n + 1, startFuel);\r\n    \/\/ For each station\r\n    for (int i = 0; i &lt; n; ++i) \r\n      \/\/ for j stops, start from high to low to avoid reusing station i\r\n      for (int j = i + 1; j &gt;= 1; --j) \r\n        if (dp[j - 1] &gt;= stations[i][0])\r\n          \/\/ if we can reach station i with j - 1 stops,\r\n          \/\/ we can reach dp[j - 1] + station[i][1] with j stops.\r\n          dp[j] = max(dp[j], dp[j - 1] + stations[i][1]);\r\n    \r\n    for (int i = 0; i &lt; dp.size(); ++i)\r\n      if (dp[i] &gt;= target) return i;\r\n    return -1;\r\n  }\r\n};<\/pre>\n<h1><strong>Solution2: Priority Queue<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int minRefuelStops(int target, int startFuel, vector&lt;vector&lt;int&gt;&gt;&amp; stations) {\r\n    int cur = startFuel;\r\n    int stops = 0;\r\n    int i = 0;\r\n    priority_queue&lt;int&gt; q; \/\/ gas (high to low) of reachable stations.\r\n    while (true) {\r\n      if (cur &gt;= target) return stops;\r\n      while (i &lt; stations.size() &amp;&amp; stations[i][0] &lt;= cur)\r\n        q.push(stations[i++][1]); \r\n      if (q.empty()) break;\r\n      cur += q.top(); q.pop();\r\n      ++stops;\r\n    }\r\n    return -1;\r\n  }\r\n};<\/pre>\n<p>V2: Iterator<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  int minRefuelStops(int target, int startFuel, vector&lt;vector&lt;int&gt;&gt;&amp; stations) {\r\n    int cur = startFuel;\r\n    int stops = 0;\r\n    priority_queue&lt;int&gt; q; \/\/ gas (high to low) of reachable stations.\r\n    auto it = begin(stations);\r\n    while (true) {\r\n      if (cur &gt;= target) return stops;\r\n      while (it != end(stations) &amp;&amp; it-&gt;at(0) &lt;= cur)\r\n        q.push(it++-&gt;at(1));\r\n      if (q.empty()) break;\r\n      cur += q.top(); q.pop();\r\n      ++stops;\r\n    }\r\n    return -1;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-322-coin-change\/\">\u82b1\u82b1\u9171 LeetCode 322. Coin Change<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem A car travels from a starting position to a destination which is\u00a0target\u00a0miles east of the starting position. Along the way, there are gas stations.\u00a0&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[337,18,195,336,72],"class_list":["post-3171","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-0-1-knapsack","tag-dp","tag-knapsack","tag-min-count","tag-priority-queue","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3171","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=3171"}],"version-history":[{"count":12,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3171\/revisions"}],"predecessor-version":[{"id":3205,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3171\/revisions\/3205"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}