Class Solution
java.lang.Object
g1501_1600.s1575_count_all_possible_routes.Solution
1575 - Count All Possible Routes.<p>Hard</p>
<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>
<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>
<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>
<p>Return <em>the count of all possible routes from</em> <code>start</code> <em>to</em> <code>finish</code>. Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> The following are all possible routes, each uses 5 units of fuel:</p>
<p>1 -> 3</p>
<p>1 -> 2 -> 3</p>
<p>1 -> 4 -> 3</p>
<p>1 -> 4 -> 2 -> 3</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> locations = [4,3,1], start = 1, finish = 0, fuel = 6</p>
<p><strong>Output:</strong> 5</p>
<p><strong>Explanation:</strong> The following are all possible routes:</p>
<p>1 -> 0, used fuel = 1</p>
<p>1 -> 2 -> 0, used fuel = 5</p>
<p>1 -> 2 -> 1 -> 0, used fuel = 5</p>
<p>1 -> 0 -> 1 -> 0, used fuel = 3</p>
<p>1 -> 0 -> 1 -> 0 -> 1 -> 0, used fuel = 5</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> locations = [5,2,1], start = 0, finish = 2, fuel = 3</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> It is impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= locations.length <= 100</code></li>
<li><code>1 <= locations[i] <= 10<sup>9</sup></code></li>
<li>All integers in <code>locations</code> are <strong>distinct</strong>.</li>
<li><code>0 <= start, finish < locations.length</code></li>
<li><code>1 <= fuel <= 200</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
countRoutes
(int[] locations, int start, int finish, int fuel)
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
countRoutes
public int countRoutes(int[] locations, int start, int finish, int fuel)
-