{"id":7341,"date":"2020-09-05T17:55:52","date_gmt":"2020-09-06T00:55:52","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7341"},"modified":"2020-09-05T18:07:48","modified_gmt":"2020-09-06T01:07:48","slug":"leetcode-1575-count-all-possible-routes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1575-count-all-possible-routes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1575. Count All Possible Routes"},"content":{"rendered":"\n<p>You are given an array of&nbsp;<strong>distinct<\/strong>&nbsp;positive integers locations&nbsp;where&nbsp;<code>locations[i]<\/code>&nbsp;represents the position of city&nbsp;<code>i<\/code>. You are also given&nbsp;integers&nbsp;<code>start<\/code>,&nbsp;<code>finish<\/code>&nbsp;and&nbsp;<code>fuel<\/code>&nbsp;representing the starting city, ending city, and the initial amount of fuel you have, respectively.<\/p>\n\n\n\n<p>At each step, if you are at city&nbsp;<code>i<\/code>, you can pick any city&nbsp;<code>j<\/code>&nbsp;such that&nbsp;<code>j != i<\/code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; locations.length<\/code>&nbsp;and move to city&nbsp;<code>j<\/code>.&nbsp;Moving from city&nbsp;<code>i<\/code>&nbsp;to city&nbsp;<code>j<\/code>&nbsp;reduces the amount of fuel you have by&nbsp;<code>|locations[i] - locations[j]|<\/code>.&nbsp;Please notice that&nbsp;<code>|x|<\/code>&nbsp;denotes the absolute value of&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p>Notice that&nbsp;<code>fuel<\/code>&nbsp;<strong>cannot<\/strong>&nbsp;become negative at any point in time, and that you are&nbsp;<strong>allowed<\/strong>&nbsp;to visit any city more than once (including&nbsp;<code>start<\/code>&nbsp;and&nbsp;<code>finish<\/code>).<\/p>\n\n\n\n<p>Return&nbsp;<em>the count of all possible routes from&nbsp;<\/em><code>start<\/code>&nbsp;<em>to<\/em>&nbsp;<code>finish<\/code>.<\/p>\n\n\n\n<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;<code>10^9 + 7<\/code>.<\/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> locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>&nbsp;The following are all possible routes, each uses 5 units of fuel:\n1 -&gt; 3\n1 -&gt; 2 -&gt; 3\n1 -&gt; 4 -&gt; 3\n1 -&gt; 4 -&gt; 2 -&gt; 3\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> locations = [4,3,1], start = 1, finish = 0, fuel = 6\n<strong>Output:<\/strong> 5\n<strong>Explanation: <\/strong>The following are all possible routes:\n1 -&gt; 0, used fuel = 1\n1 -&gt; 2 -&gt; 0, used fuel = 5\n1 -&gt; 2 -&gt; 1 -&gt; 0, used fuel = 5\n1 -&gt; 0 -&gt; 1 -&gt; 0, used fuel = 3\n1 -&gt; 0 -&gt; 1 -&gt; 0 -&gt; 1 -&gt; 0, used fuel = 5\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> locations = [5,2,1], start = 0, finish = 2, fuel = 3\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>It's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel.<\/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> locations = [2,1,5], start = 0, finish = 0, fuel = 3\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>&nbsp;There are two possible routes, 0 and 0 -&gt; 1 -&gt; 0.<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> locations = [1,2,3], start = 0, finish = 2, fuel = 40\n<strong>Output:<\/strong> 615088286\n<strong>Explanation: <\/strong>The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= locations.length &lt;= 100<\/code><\/li><li><code>1 &lt;= locations[i] &lt;= 10^9<\/code><\/li><li>All integers in&nbsp;<code>locations<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><li><code>0 &lt;= start, finish &lt;&nbsp;locations.length<\/code><\/li><li><code>1 &lt;= fuel &lt;= 200<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[j][f] := # of ways to start from city &#8216;start&#8217; to reach city &#8216;j&#8217; with fuel level f.<\/p>\n\n\n\n<p>dp[j][f] = sum(dp[i][f + d])  d = dist(i, j)<\/p>\n\n\n\n<p>init: dp[start][fuel] = 1<\/p>\n\n\n\n<p>Time complexity: O(n^2*fuel)<br>Space complexity: O(n*fuel)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int countRoutes(vector<int>& x, int start, int finish, int fuel) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = x.size();    \n    vector<vector<int>> dp(n, vector<int>(fuel + 1));\n    dp[start][fuel] = 1;\n    for (int f = fuel; f > 0; --f)\n      for (int i = 0; i < n; ++i) {\n        if (!dp[i][f] || abs(x[i] - x[finish]) > f) continue; \/\/ pruning.\n        for (int j = 0; j < n; ++j) {\n          const int d = abs(x[i] - x[j]);                    \n          if (i == j || f < d) continue;\n          dp[j][f - d] = (dp[j][f - d] + dp[i][f]) % kMod;\n        }\n      }\n    return accumulate(begin(dp[finish]), end(dp[finish]), 0LL) % kMod;        \n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python3\">\n# Author: Huahua\nclass Solution:\n  def countRoutes(self, x: List[int], start: int, finish: int, fuel: int) -> int:    \n    @lru_cache(None)\n    def dp(i, f): # ways to reach |finsh| from |i| with |f| fuel.\n      if f < 0: return 0\n      return (sum(dp(j, f - abs(x[i] - x[j])) \n                 for j in range(len(x)) if i != j) + (i == finish)) % (10**9 + 7)\n    return dp(start, fuel)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of&nbsp;distinct&nbsp;positive integers locations&nbsp;where&nbsp;locations[i]&nbsp;represents the position of city&nbsp;i. You are also given&nbsp;integers&nbsp;start,&nbsp;finish&nbsp;and&nbsp;fuel&nbsp;representing the starting city, ending city, and the initial amount&#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":[18,217],"class_list":["post-7341","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7341","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=7341"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7341\/revisions"}],"predecessor-version":[{"id":7344,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7341\/revisions\/7344"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}