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 fuelcannot 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 fromstarttofinish. Since the answer may be too large, return it modulo 10^9 + 7.
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:
1
2
3
4
5
6
7
8
9
10
Input:
locations =[4,3,1], start =1, finish =0, fuel =6Output:
5Explanation: The following are all possible routes:1->0, used fuel =11->2->0, used fuel =51->2->1->0, used fuel =51->0->1->0, used fuel =31->0->1->0->1->0, used fuel =5
Example 3:
1
2
3
4
5
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.
The problem asks for the number of possible routes from start to finish using at most fuel units, possibly revisiting cities. Since each move reduces fuel and cities can be revisited, the problem has overlapping subproblems and optimal substructure, making it suitable for DP with memoization. At each city with a given fuel, the answer depends only on the current city and remaining fuel.
Define a recursive function dfs(i, f) that returns the number of ways to reach finish from city i with f fuel left.
If f < 0, return 0 (not enough fuel).
If at city i with f fuel, initialize ans as 1 if i == finish (can stay at finish).
For each other city j, if moving from i to j costs cost = abs(locations[i] - locations[j]) and f >= cost, recursively call dfs(j, f - cost) and add to ans.
Use memoization to cache results for (i, f) to avoid recomputation.
classSolution {
public:int countRoutes(vector<int>& loc, int start, int finish, int fuel) {
int n = loc.size(), MOD =1e9+7;
vector<vector<int>> dp(n, vector<int>(fuel+1, -1));
function<int(int,int)> dfs = [&](int i, int f) {
if (f <0) return0;
if (dp[i][f] !=-1) return dp[i][f];
int ans = (i == finish) ?1:0;
for (int j =0; j < n; ++j) {
if (j == i) continue;
int cost = abs(loc[i] - loc[j]);
if (f >= cost)
ans = (ans + dfs(j, f - cost)) % MOD;
}
return dp[i][f] = ans;
};
returndfs(start, fuel);
}
};
classSolution {
staticfinalint MOD = 1_000_000_007;
publicintcountRoutes(int[] loc, int start, int finish, int fuel) {
int n = loc.length;
Integer[][] dp =new Integer[n][fuel+1];
return dfs(loc, start, finish, fuel, dp);
}
intdfs(int[] loc, int i, int finish, int f, Integer[][] dp) {
if (f < 0) return 0;
if (dp[i][f]!=null) return dp[i][f];
int ans = (i == finish) ? 1 : 0;
for (int j = 0; j < loc.length; ++j) {
if (j == i) continue;
int cost = Math.abs(loc[i]- loc[j]);
if (f >= cost)
ans = (ans + dfs(loc, j, finish, f - cost, dp)) % MOD;
}
return dp[i][f]= ans;
}
}
classSolution:
defcountRoutes(self, loc: list[int], start: int, finish: int, fuel: int) -> int:
MOD =10**9+7 n = len(loc)
from functools import cache
@cachedefdfs(i: int, f: int) -> int:
if f <0:
return0 ans =1if i == finish else0for j in range(n):
if j == i:
continue cost = abs(loc[i] - loc[j])
if f >= cost:
ans = (ans + dfs(j, f - cost)) % MOD
return ans
return dfs(start, fuel)
Instead of solving subproblems recursively, we can build up the solution iteratively using a DP table. For each city and each possible fuel amount, we compute the number of ways to reach the finish city. This avoids recursion and stack overhead, and makes the state transitions explicit.
classSolution {
public:int countRoutes(vector<int>& loc, int start, int finish, int fuel) {
int n = loc.size(), MOD =1e9+7;
vector<vector<int>> dp(n, vector<int>(fuel+1, 0));
for (int f =0; f <= fuel; ++f)
dp[finish][f] =1;
for (int f =0; f <= fuel; ++f) {
for (int i =0; i < n; ++i) {
for (int j =0; j < n; ++j) {
if (i == j) continue;
int cost = abs(loc[i] - loc[j]);
if (f >= cost)
dp[i][f] = (dp[i][f] + dp[j][f - cost]) % MOD;
}
}
}
return dp[start][fuel];
}
};
classSolution {
staticfinalint MOD = 1_000_000_007;
publicintcountRoutes(int[] loc, int start, int finish, int fuel) {
int n = loc.length;
int[][] dp =newint[n][fuel+1];
for (int f = 0; f <= fuel; ++f)
dp[finish][f]= 1;
for (int f = 0; f <= fuel; ++f) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j) continue;
int cost = Math.abs(loc[i]- loc[j]);
if (f >= cost)
dp[i][f]= (dp[i][f]+ dp[j][f - cost]) % MOD;
}
}
}
return dp[start][fuel];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
privateval MOD = 1_000_000_007
funcountRoutes(loc: IntArray, start: Int, finish: Int, fuel: Int): Int {
val n = loc.size
val dp = Array(n) { IntArray(fuel + 1) }
for (f in0..fuel) dp[finish][f] = 1for (f in0..fuel) {
for (i in0 until n) {
for (j in0 until n) {
if (i == j) continueval cost = kotlin.math.abs(loc[i] - loc[j])
if (f >= cost)
dp[i][f] = (dp[i][f] + dp[j][f - cost]) % MOD
}
}
}
return dp[start][fuel]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defcountRoutes(self, loc: list[int], start: int, finish: int, fuel: int) -> int:
MOD =10**9+7 n = len(loc)
dp: list[list[int]] = [[0] * (fuel +1) for _ in range(n)]
for f in range(fuel +1):
dp[finish][f] =1for f in range(fuel +1):
for i in range(n):
for j in range(n):
if i == j:
continue cost = abs(loc[i] - loc[j])
if f >= cost:
dp[i][f] = (dp[i][f] + dp[j][f - cost]) % MOD
return dp[start][fuel]