-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount All Possible Routes.java
More file actions
34 lines (30 loc) · 1017 Bytes
/
Count All Possible Routes.java
File metadata and controls
34 lines (30 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Solution {
int[][] dp;
int n;
int finish;
public int solve(int[] locations, int currCity, int remainingFuel) {
if (remainingFuel < 0) {
return 0;
}
if (dp[currCity][remainingFuel] != -1) {
return dp[currCity][remainingFuel];
}
int ans = currCity == finish ? 1 : 0;
for (int nextCity = 0; nextCity < n; nextCity++) {
if (nextCity != currCity) {
ans = (ans + solve(locations, nextCity,
remainingFuel - Math.abs(locations[currCity] - locations[nextCity]))) % 1000000007;
}
}
return dp[currCity][remainingFuel] = ans;
}
public int countRoutes(int[] locations, int start, int finish, int fuel) {
n = locations.length;
dp = new int[n][fuel + 1];
this.finish = finish;
for (int i = 0; i < n; ++i) {
Arrays.fill(dp[i], -1);
}
return solve(locations, start, fuel);
}
}