5
1. We will start from start pos provided and will dfs travel to each other location .6
2. each time we will see if we reach finish , we will increment the result . but we wont stop there if we have fuel left and continue travelling7
3. if fuel goes negetive , we will return 0 , as there is no valid solution in that path8
4. we will take dp[locations][fuel+1] to cache the result , to avoid recomputing .14
int mod = (int) Math.pow(10, 9) + 7;17
public int countRoutes(int[] locations, int start, int finish, int fuel) {19
dp = new int[locations.length][fuel + 1];21
for (int[] row : dp) {25
return dfs(locations, start, finish, fuel);28
public int dfs(int[] locations, int cur_location, int finish, int fuel) {34
if (dp[cur_location][fuel] != -1) {35
return dp[cur_location][fuel];40
if (cur_location == finish) {44
for (int i = 0; i < locations.length; i++) {46
if (i == cur_location) continue;48
int fuel_cost = Math.abs(locations[i] - locations[cur_location]);49
int next_trip = dfs(locations, i, finish, fuel - fuel_cost);54
dp[cur_location][fuel] = result;56
return dp[cur_location][fuel];