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 = 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
1 <= locations[i] <= 109
- All integers in
locations
are distinct. 0 <= start, finish < locations.length
1 <= fuel <= 200
We design a function
The process of calculating the function
- If
$k \lt |locations[i] - locations[finish]|$ , then return$0$ . - If
$i = finish$ , then the number of paths is$1$ at the beginning, otherwise it is$0$ . - Then, we traverse all cities
$j$ . If$j \ne i$ , then we can move from city$i$ to city$j$ , and the remaining fuel is$k - |locations[i] - locations[j]|$ . Then we can add the number of paths to the answer$dfs(j, k - |locations[i] - locations[j]|)$ . - Finally, we return the number of paths to the answer.
To avoid repeated calculations, we can use memoization.
The time complexity is
class Solution:
def countRoutes(
self, locations: List[int], start: int, finish: int, fuel: int
) -> int:
@cache
def dfs(i: int, k: int) -> int:
if k < abs(locations[i] - locations[finish]):
return 0
ans = int(i == finish)
for j, x in enumerate(locations):
if j != i:
ans = (ans + dfs(j, k - abs(locations[i] - x))) % mod
return ans
mod = 10**9 + 7
return dfs(start, fuel)
class Solution {
private int[] locations;
private int finish;
private int n;
private Integer[][] f;
private final int mod = (int) 1e9 + 7;
public int countRoutes(int[] locations, int start, int finish, int fuel) {
n = locations.length;
this.locations = locations;
this.finish = finish;
f = new Integer[n][fuel + 1];
return dfs(start, fuel);
}
private int dfs(int i, int k) {
if (k < Math.abs(locations[i] - locations[finish])) {
return 0;
}
if (f[i][k] != null) {
return f[i][k];
}
int ans = i == finish ? 1 : 0;
for (int j = 0; j < n; ++j) {
if (j != i) {
ans = (ans + dfs(j, k - Math.abs(locations[i] - locations[j]))) % mod;
}
}
return f[i][k] = ans;
}
}
class Solution {
public:
int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
int n = locations.size();
int f[n][fuel + 1];
memset(f, -1, sizeof(f));
const int mod = 1e9 + 7;
function<int(int, int)> dfs = [&](int i, int k) -> int {
if (k < abs(locations[i] - locations[finish])) {
return 0;
}
if (f[i][k] != -1) {
return f[i][k];
}
int ans = i == finish;
for (int j = 0; j < n; ++j) {
if (j != i) {
ans = (ans + dfs(j, k - abs(locations[i] - locations[j]))) % mod;
}
}
return f[i][k] = ans;
};
return dfs(start, fuel);
}
};
func countRoutes(locations []int, start int, finish int, fuel int) int {
n := len(locations)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, fuel+1)
for j := range f[i] {
f[i][j] = -1
}
}
const mod = 1e9 + 7
var dfs func(int, int) int
dfs = func(i, k int) (ans int) {
if k < abs(locations[i]-locations[finish]) {
return 0
}
if f[i][k] != -1 {
return f[i][k]
}
if i == finish {
ans = 1
}
for j, x := range locations {
if j != i {
ans = (ans + dfs(j, k-abs(locations[i]-x))) % mod
}
}
f[i][k] = ans
return
}
return dfs(start, fuel)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
function countRoutes(locations: number[], start: number, finish: number, fuel: number): number {
const n = locations.length;
const f = Array.from({ length: n }, () => Array(fuel + 1).fill(-1));
const mod = 1e9 + 7;
const dfs = (i: number, k: number): number => {
if (k < Math.abs(locations[i] - locations[finish])) {
return 0;
}
if (f[i][k] !== -1) {
return f[i][k];
}
let ans = i === finish ? 1 : 0;
for (let j = 0; j < n; ++j) {
if (j !== i) {
const x = Math.abs(locations[i] - locations[j]);
ans = (ans + dfs(j, k - x)) % mod;
}
}
return (f[i][k] = ans);
};
return dfs(start, fuel);
}
We can also convert the memoization of solution 1 into dynamic programming.
We define
Next, we enumerate the remaining fuel
Finally, we return the number of paths to the answer
The time complexity is
class Solution:
def countRoutes(
self, locations: List[int], start: int, finish: int, fuel: int
) -> int:
mod = 10**9 + 7
n = len(locations)
f = [[0] * (fuel + 1) for _ in range(n)]
for k in range(fuel + 1):
f[finish][k] = 1
for k in range(fuel + 1):
for i in range(n):
for j in range(n):
if j != i and abs(locations[i] - locations[j]) <= k:
f[i][k] = (
f[i][k] + f[j][k - abs(locations[i] - locations[j])]
) % mod
return f[start][fuel]
class Solution {
public int countRoutes(int[] locations, int start, int finish, int fuel) {
final int mod = (int) 1e9 + 7;
int n = locations.length;
int[][] f = new int[n][fuel + 1];
for (int k = 0; k <= fuel; ++k) {
f[finish][k] = 1;
}
for (int k = 0; k <= fuel; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != i && Math.abs(locations[i] - locations[j]) <= k) {
f[i][k] = (f[i][k] + f[j][k - Math.abs(locations[i] - locations[j])]) % mod;
}
}
}
}
return f[start][fuel];
}
}
class Solution {
public:
int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
const int mod = 1e9 + 7;
int n = locations.size();
int f[n][fuel + 1];
memset(f, 0, sizeof(f));
for (int k = 0; k <= fuel; ++k) {
f[finish][k] = 1;
}
for (int k = 0; k <= fuel; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != i && abs(locations[i] - locations[j]) <= k) {
f[i][k] = (f[i][k] + f[j][k - abs(locations[i] - locations[j])]) % mod;
}
}
}
}
return f[start][fuel];
}
};
func countRoutes(locations []int, start int, finish int, fuel int) int {
n := len(locations)
const mod = 1e9 + 7
f := make([][]int, n)
for i := range f {
f[i] = make([]int, fuel+1)
}
for k := 0; k <= fuel; k++ {
f[finish][k] = 1
}
for k := 0; k <= fuel; k++ {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if j != i && abs(locations[i]-locations[j]) <= k {
f[i][k] = (f[i][k] + f[j][k-abs(locations[i]-locations[j])]) % mod
}
}
}
}
return f[start][fuel]
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
function countRoutes(locations: number[], start: number, finish: number, fuel: number): number {
const n = locations.length;
const f = Array.from({ length: n }, () => Array(fuel + 1).fill(0));
for (let k = 0; k <= fuel; ++k) {
f[finish][k] = 1;
}
const mod = 1e9 + 7;
for (let k = 0; k <= fuel; ++k) {
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
if (j !== i && Math.abs(locations[i] - locations[j]) <= k) {
f[i][k] = (f[i][k] + f[j][k - Math.abs(locations[i] - locations[j])]) % mod;
}
}
}
}
return f[start][fuel];
}