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.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#define MOD 1000000007
6
7int dp[101][201];
8
9int countRoutesUtil(int* locations, int n, int curr, int finish, int fuel) {
10 if (fuel < 0) return 0;
11 if (dp[curr][fuel] != -1) return dp[curr][fuel];
12
13 int result = (curr == finish) ? 1 : 0;
14 for (int i = 0; i < n; ++i) {
15 if (i != curr) {
16 result = (result + countRoutesUtil(locations, n, i, finish, fuel - abs(locations[i] - locations[curr]))) % MOD;
17 }
18 }
19 return dp[curr][fuel] = result;
20}
21
22int countRoutes(int* locations, int locationsSize, int start, int finish, int fuel) {
23 memset(dp, -1, sizeof(dp));
24 return countRoutesUtil(locations, locationsSize, start, finish, fuel);
25}
26
27int main() {
28 int locations[] = {2, 3, 6, 8, 4};
29 int start = 1, finish = 3, fuel = 5;
30 printf("%d\n", countRoutes(locations, 5, start, finish, fuel)); // Output: 4
31 return 0;
32}
The C solution uses a top-down dynamic programming approach with memoization. It initializes a 2D array (dp) to store the number of routes. The recursive function explores from the current city to possible destinations, decrementing the fuel and checking if it reaches the finish city. Modulo operation by 10^9 + 7 is applied to avoid overflow.
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.
1
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.