Class Solution
-
- All Implemented Interfaces:
public final class Solution1575 - Count All Possible Routes\.
Hard
You are given an array of distinct positive integers locations where
locations[i]represents the position of cityi. You are also given integersstart,finishandfuelrepresenting the starting city, ending city, and the initial amount of fuel you have, respectively.At each step, if you are at city
i, you can pick any cityjsuch thatj != iand0 <= j < locations.lengthand move to cityj. Moving from cityito cityjreduces the amount of fuel you have by|locations[i] - locations[j]|. Please notice that|x|denotes the absolute value ofx.Notice that
fuelcannot become negative at any point in time, and that you are allowed to visit any city more than once (includingstartandfinish).Return the count of all possible routes from
starttofinish. Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.Example 1:
Input: locations = 2,3,6,8,4, start = 1, finish = 3, fuel = 5
Output: 4
Explanation: The following are all possible routes, each uses 5 units of fuel:
1 -> 3
1 -> 2 -> 3
1 -> 4 -> 3
1 -> 4 -> 2 -> 3
Example 2:
Input: locations = 4,3,1, start = 1, finish = 0, fuel = 6
Output: 5
Explanation: The following are all possible routes:
1 -> 0, used fuel = 1
1 -> 2 -> 0, used fuel = 5
1 -> 2 -> 1 -> 0, used fuel = 5
1 -> 0 -> 1 -> 0, used fuel = 3
1 -> 0 -> 1 -> 0 -> 1 -> 0, used fuel = 5
Example 3:
Input: locations = 5,2,1, start = 0, finish = 2, fuel = 3
Output: 0
Explanation: It is impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel.
Constraints:
2 <= locations.length <= 100<code>1 <= locationsi<= 10<sup>9</sup></code>
All integers in
locationsare distinct.0 <= start, finish < locations.length1 <= fuel <= 200
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classSolution.Companion
-
Field Summary
Fields Modifier and Type Field Description public final static Solution.CompanionCompanion
-
Constructor Summary
Constructors Constructor Description Solution()
-