Sponsored
Sponsored
This approach involves using dynamic programming to keep track of the number of ways to reach the target city with the available fuel. We will define a DP table where dp[i][f] represents the number of ways to start from city i with f units of fuel left and reach the target city. We recursively fill this table while considering moves to any other city and subtracting the appropriate fuel cost for that move. The result will be stored in the dp[start][fuel] after exploring all possible routes.
Time Complexity: O(n * fuel * n), where n is the number of cities. This can be reduced using better states or pruning.
Space Complexity: O(n * fuel) for the dp table.
1class CountRoutes:
2 MOD = 10**9 + 7
3
4 def __init__(self):
5 self.dp = []
6
7 def countRoutes(self, locations, start, finish, fuel):
8 n = len(locations)
9 self.dp = [[-1] * (fuel + 1) for _ in range(n)]
10 return self.countRoutesUtil(locations, start, finish, fuel)
11
12 def countRoutesUtil(self, locations, curr, finish, fuel):
13 if fuel < 0:
14 return 0
15 if self.dp[curr][fuel] != -1:
16 return self.dp[curr][fuel]
17
18 result = 1 if curr == finish else 0
19 for i in range(len(locations)):
20 if i != curr:
21 result = (result + self.countRoutesUtil(locations, i, finish, fuel - abs(locations[i] - locations[curr]))) % self.MOD
22
23 self.dp[curr][fuel] = result
24 return result
25
26# Example usage
27challenge = CountRoutes()
28locations = [2, 3, 6, 8, 4]
29start, finish, fuel = 1, 3, 5
30print(challenge.countRoutes(locations, start, finish, fuel)) # Output: 4
The Python implementation uses a class to store the DP table and facilitates direct recursion with memoization. It iterates over options in recursive calls while updating the DP table and returns results modulo 10^9 + 7.
In this approach, a depth-first search (DFS) is implemented to explore possible routes from the start to the finish city. Each travel option is a branch in the DFS tree, where the algorithm explores each choice of stopping at a different city until it no longer has fuel. The recursion keeps track of current fuel and updates the count whenever it reaches the finish city with valid fuel.
Time Complexity: O(n^fuel) without pruning, given recursive branching per city. This complexity is potentially high without memoization.
Space Complexity: O(fuel) due to recursive call stack depth.
class DFSCountRoutes {
const int MOD = 1000000007;
public int CountRoutes(int[] locations, int start, int finish, int fuel) {
return countRoutesDFS(locations, start, finish, fuel);
}
private int countRoutesDFS(int[] locations, int curr, int finish, int fuel) {
if (fuel < 0) return 0;
int result = (curr == finish) ? 1 : 0;
for (int i = 0; i < locations.Length; i++) {
if (i != curr) {
result = (result + countRoutesDFS(locations, i, finish, fuel - Math.Abs(locations[i] - locations[curr]))) % MOD;
}
}
return result;
}
static void Main() {
int[] locations = {2, 3, 6, 8, 4};
DFSCountRoutes obj = new DFSCountRoutes();
Console.WriteLine(obj.CountRoutes(locations, 1, 3, 5)); // Output: 4
}
}
The C# solution implements DFS recursively, evaluating new city options as fuel allows. Each branch solution aggregates the result for possible paths, using a modulus for output restraint.