|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../shortest-subarray-to-be-removed-to-make-array-sorted "Shortest Subarray to be Removed to Make Array Sorted") |
| 9 | + |
| 10 | +[Next >](../replace-all-s-to-avoid-consecutive-repeating-characters "Replace All ?'s to Avoid Consecutive Repeating Characters") |
| 11 | + |
| 12 | +## [1575. Count All Possible Routes (Hard)](https://leetcode.com/problems/count-all-possible-routes "统计所有可行路径") |
| 13 | + |
| 14 | +<p>You are given an array of <strong>distinct</strong> positive integers locations where <code>locations[i]</code> represents the position of city <code>i</code>. You are also given integers <code>start</code>, <code>finish</code> and <code>fuel</code> representing the starting city, ending city, and the initial amount of fuel you have, respectively.</p> |
| 15 | + |
| 16 | +<p>At each step, if you are at city <code>i</code>, you can pick any city <code>j</code> such that <code>j != i</code> and <code>0 <= j < locations.length</code> and move to city <code>j</code>. Moving from city <code>i</code> to city <code>j</code> reduces the amount of fuel you have by <code>|locations[i] - locations[j]|</code>. Please notice that <code>|x|</code> denotes the absolute value of <code>x</code>.</p> |
| 17 | + |
| 18 | +<p>Notice that <code>fuel</code> <strong>cannot</strong> become negative at any point in time, and that you are <strong>allowed</strong> to visit any city more than once (including <code>start</code> and <code>finish</code>).</p> |
| 19 | + |
| 20 | +<p>Return <em>the count of all possible routes from </em><code>start</code> <em>to</em> <code>finish</code>.</p> |
| 21 | + |
| 22 | +<p>Since the answer may be too large, return it modulo <code>10^9 + 7</code>.</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong>Example 1:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5 |
| 29 | +<strong>Output:</strong> 4 |
| 30 | +<strong>Explanation:</strong> The following are all possible routes, each uses 5 units of fuel: |
| 31 | +1 -> 3 |
| 32 | +1 -> 2 -> 3 |
| 33 | +1 -> 4 -> 3 |
| 34 | +1 -> 4 -> 2 -> 3 |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong>Example 2:</strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> locations = [4,3,1], start = 1, finish = 0, fuel = 6 |
| 41 | +<strong>Output:</strong> 5 |
| 42 | +<strong>Explanation: </strong>The following are all possible routes: |
| 43 | +1 -> 0, used fuel = 1 |
| 44 | +1 -> 2 -> 0, used fuel = 5 |
| 45 | +1 -> 2 -> 1 -> 0, used fuel = 5 |
| 46 | +1 -> 0 -> 1 -> 0, used fuel = 3 |
| 47 | +1 -> 0 -> 1 -> 0 -> 1 -> 0, used fuel = 5 |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p><strong>Example 3:</strong></p> |
| 51 | + |
| 52 | +<pre> |
| 53 | +<strong>Input:</strong> locations = [5,2,1], start = 0, finish = 2, fuel = 3 |
| 54 | +<strong>Output:</strong> 0 |
| 55 | +<b>Explanation: </b>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> |
| 56 | + |
| 57 | +<p><strong>Example 4:</strong></p> |
| 58 | + |
| 59 | +<pre> |
| 60 | +<strong>Input:</strong> locations = [2,1,5], start = 0, finish = 0, fuel = 3 |
| 61 | +<strong>Output:</strong> 2 |
| 62 | +<strong>Explanation:</strong> There are two possible routes, 0 and 0 -> 1 -> 0.</pre> |
| 63 | + |
| 64 | +<p><strong>Example 5:</strong></p> |
| 65 | + |
| 66 | +<pre> |
| 67 | +<strong>Input:</strong> locations = [1,2,3], start = 0, finish = 2, fuel = 40 |
| 68 | +<strong>Output:</strong> 615088286 |
| 69 | +<strong>Explanation: </strong>The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286. |
| 70 | +</pre> |
| 71 | + |
| 72 | +<p> </p> |
| 73 | +<p><strong>Constraints:</strong></p> |
| 74 | + |
| 75 | +<ul> |
| 76 | + <li><code>2 <= locations.length <= 100</code></li> |
| 77 | + <li><code>1 <= locations[i] <= 10^9</code></li> |
| 78 | + <li>All integers in <code>locations</code> are <strong>distinct</strong>.</li> |
| 79 | + <li><code>0 <= start, finish < locations.length</code></li> |
| 80 | + <li><code><font face="monospace">1 <= fuel <= 200</font></code></li> |
| 81 | +</ul> |
| 82 | + |
| 83 | +### Related Topics |
| 84 | + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] |
| 85 | + |
| 86 | +### Hints |
| 87 | +<details> |
| 88 | +<summary>Hint 1</summary> |
| 89 | +Use dynamic programming to solve this problem with each state defined by the city index and fuel left. |
| 90 | +</details> |
| 91 | + |
| 92 | +<details> |
| 93 | +<summary>Hint 2</summary> |
| 94 | +Since the array contains distinct integers fuel will always be spent in each move and so there can be no cycles. |
| 95 | +</details> |
0 commit comments