You are given an array of distinct positive integers locations where locations[i] represents the position of city i. You are also given integers start, finish and fuel representing 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 city j such that j != i and 0 <= j < locations.length and move to city j. Moving from city i to city j reduces the amount of fuel you have by |locations[i] - locations[j]|. Please notice that |x| denotes the absolute value of x.
Notice that fuel cannot become negative at any point in time, and that you are allowed to visit any city more than once (including start and finish).
Return the count of all possible routes from start to finish. Since the answer may be too large, return it modulo 109 + 7.
Example 1:
Input: locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5Output: 4Explanation: 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 = 6Output: 5Explanation: 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 = 3Output: 0Explanation: 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
- 1 <= locations[i] <= 109
- All integers in locations are distinct.
- 0 <= start, finish < locations.length
- 1 <= fuel <= 200
翻译:
- 计算所有的可能路线
你被给出了一组不同的正整数位置的数组其中locations[i]代表城市i的位置。你也被给了开始的整数start,finish和fuel分别代表着开始的城市,结束的城市和你拥有的最初的燃料数量
在每一步中,如果你处于城市i,你能够选择任何一个城市j但j!=i且0<=j<=locations的
长度并且移动到城市j。从城市i移动到城市j消耗的燃料数量为|locations[i] - locations[j]|
请注意|x|表示x的绝对值。
注意燃料在任何时间内都不能变成负数,并且你被允许参观一个城市多于一次(包括开始和结束)
返回所有从开始到结束的可能路线的数量,如果答案的数目太大的话,请返回它对1e9+7的模。
例1:
输入:locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
输出:4
注释:以下所有可能的路线,每个使用5个单位的燃料:
1 -> 3
1 -> 2 -> 3
1 -> 4 -> 3
1 -> 4 -> 2 -> 3
例2:
输入:locations = [4,3,1], start = 1, finish = 0, fuel = 6
输出:5
注释:以下所有可能的路线:
1 -> 0, 使用燃料 = 1
1 -> 2 -> 0,使用燃料 = 5
1 -> 2 -> 1 -> 0, 使用燃料 = 5
1 -> 0 -> 1 -> 0, 使用燃料 = 3
1 -> 0 -> 1 -> 0 -> 1 -> 0, 使用燃料 = 5
例3:
输入:locations = [5,2,1], start = 0, finish = 2, fuel = 3
输出;0
注释:从0到2可能仅仅使用3个单位的燃料虽然最短路线仅仅需要4个单位
限制:
- 2 <= locations.length <= 100
- 1 <= locations[i] <= 109
- 所有在 locations中的整数都是不同的.
- 0 <= start, finish < locations.length
- 1 <= fuel <= 200