Skip to content

Commit bf0e884

Browse files
committed
Runtime: 81 ms (Top 78.44%) | Memory: 41.8 MB (Top 95.21%)
1 parent b2eed39 commit bf0e884

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 81 ms (Top 78.44%) | Memory: 41.8 MB (Top 95.21%)
12

23
/*
34
@@ -10,52 +11,50 @@
1011
1112
*/
1213

13-
1414
class Solution {
15-
15+
1616
int mod = (int)Math.pow(10,9) + 7 ;
1717
int[][] dp ;
18-
18+
1919
public int countRoutes(int[] locations, int start, int finish, int fuel) {
20-
20+
2121
dp = new int[locations.length][fuel+1] ;
22-
22+
2323
for(int[] row : dp){
2424
Arrays.fill(row , -1) ;
2525
}
26-
26+
2727
return dfs(locations , start , finish , fuel);
2828
}
29-
29+
3030
public int dfs(int[] locations , int cur_location , int finish , int fuel){
31-
31+
3232
if(fuel < 0){
3333
return 0 ;
3434
}
35-
35+
3636
if(dp[cur_location][fuel] != -1){
3737
return dp[cur_location][fuel] ;
3838
}
39-
39+
4040
int result = 0 ;
41-
41+
4242
if(cur_location == finish){
4343
result++ ;
4444
}
45-
45+
4646
for(int i=0 ; i<locations.length ; i++){
47-
47+
4848
if(i == cur_location) continue ;
49-
50-
int fuel_cost = Math.abs(locations[i] - locations[cur_location]);
49+
50+
int fuel_cost = Math.abs(locations[i] - locations[cur_location]);
5151
int next_trip = dfs(locations , i , finish , fuel-fuel_cost);
5252
result += next_trip ;
5353
result %= mod ;
5454
}
55-
55+
5656
dp[cur_location][fuel] = result ;
57-
57+
5858
return dp[cur_location][fuel] ;
5959
}
6060
}
61-

0 commit comments

Comments
 (0)