72
72
73
73
函数 $dfs(i, k)$ 的计算过程如下:
74
74
75
- - 如果 $k \lt 0$ 或者 $ |locations[ i] - locations[ finish] | \gt k $,那么返回 $0$。
75
+ - 如果 $k \lt |locations[ i] - locations[ finish] |$,那么返回 $0$。
76
76
- 如果 $i = finish$,那么答案路径数初始时为 $1$,否则为 $0$。
77
77
- 然后,我们遍历所有城市 $j$,如果 $j \ne i$,那么我们可以从城市 $i$ 移动到城市 $j$,此时剩余汽油量为 $k - |locations[ i] - locations[ j] |$,那么我们可以将答案路径数加上 $dfs(j, k - |locations[ i] - locations[ j] |)$。
78
78
- 最后,我们返回答案路径数。
79
79
80
80
为了避免重复计算,我们可以使用记忆化搜索。
81
81
82
- 时间复杂度 $O(n^2 \times fuel)$,空间复杂度 $O(n \times fuel)$。其中 $n$ 为城市数量。
82
+ 时间复杂度 $O(n^2 \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别是数组 $locations$ 和 $fuel$ 的大小。
83
+
84
+ ** 方法二:动态规划**
85
+
86
+ 我们也可以将方法一的记忆化搜索转换为动态规划。
87
+
88
+ 我们定义 $f[ i] [ k ] $ 表示从城市 $i$ 出发,剩余汽油量为 $k$ 时,到达目的地 $finish$ 的路径数。那么答案就是 $f[ start] [ fuel ] $。初始时 $f[ finish] [ k ] =1$,其余均为 $0$。
89
+
90
+ 接下来,我们从小到大枚举剩余汽油量 $k$,然后枚举所有的城市 $i$,对于每个城市 $i$,我们枚举所有的城市 $j$,如果 $j \ne i$,并且 $|locations[ i] - locations[ j] | \le k$,那么我们可以从城市 $i$ 移动到城市 $j$,此时剩余汽油量为 $k - |locations[ i] - locations[ j] |$,那么我们可以将答案路径数加上 $f[ j] [ k - |locations[ i] - locations[ j] |] $。
91
+
92
+ 最后,我们返回答案路径数 $f[ start] [ fuel ] $ 即可。
93
+
94
+ 时间复杂度 $O(n^2 \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别是数组 $locations$ 和 $fuel$ 的大小。
83
95
84
96
<!-- tabs:start -->
85
97
89
101
90
102
``` python
91
103
class Solution :
92
- def countRoutes (
93
- self , locations : List[int ], start : int , finish : int , fuel : int
94
- ) -> int :
104
+ def countRoutes (self , locations : List[int ], start : int , finish : int , fuel : int ) -> int :
95
105
@cache
96
106
def dfs (i : int , k : int ) -> int :
97
- if k < 0 or abs (locations[i] - locations[finish] > k ):
107
+ if k < abs (locations[i] - locations[finish]):
98
108
return 0
99
109
ans = int (i == finish)
100
- ans += sum (
101
- dfs(j, k - abs (locations[i] - x))
102
- for j, x in enumerate (locations)
103
- if j != i
104
- )
105
- return ans % mod
110
+ for j, x in enumerate (locations):
111
+ if j != i:
112
+ ans = (ans + dfs(j, k - abs (locations[i] - x))) % mod
113
+ return ans
106
114
107
115
mod = 10 ** 9 + 7
108
116
return dfs(start, fuel)
109
117
```
110
118
119
+ ``` python
120
+ class Solution :
121
+ def countRoutes (self , locations : List[int ], start : int , finish : int , fuel : int ) -> int :
122
+ mod = 10 ** 9 + 7
123
+ n = len (locations)
124
+ f = [[0 ] * (fuel + 1 ) for _ in range (n)]
125
+ for k in range (fuel + 1 ):
126
+ f[finish][k] = 1
127
+ for k in range (fuel + 1 ):
128
+ for i in range (n):
129
+ for j in range (n):
130
+ if j != i and abs (locations[i] - locations[j]) <= k:
131
+ f[i][k] = (f[i][k] + f[j][k - abs (locations[i] - locations[j])]) % mod
132
+ return f[start][fuel]
133
+ ```
134
+
111
135
### ** Java**
112
136
113
137
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -129,7 +153,7 @@ class Solution {
129
153
}
130
154
131
155
private int dfs (int i , int k ) {
132
- if (k < 0 || Math . abs(locations[i] - locations[finish]) > k ) {
156
+ if (k < Math . abs(locations[i] - locations[finish])) {
133
157
return 0 ;
134
158
}
135
159
if (f[i][k] != null ) {
@@ -146,6 +170,29 @@ class Solution {
146
170
}
147
171
```
148
172
173
+ ``` java
174
+ class Solution {
175
+ public int countRoutes (int [] locations , int start , int finish , int fuel ) {
176
+ final int mod = (int ) 1e9 + 7 ;
177
+ int n = locations. length;
178
+ int [][] f = new int [n][fuel + 1 ];
179
+ for (int k = 0 ; k <= fuel; ++ k) {
180
+ f[finish][k] = 1 ;
181
+ }
182
+ for (int k = 0 ; k <= fuel; ++ k) {
183
+ for (int i = 0 ; i < n; ++ i) {
184
+ for (int j = 0 ; j < n; ++ j) {
185
+ if (j != i && Math . abs(locations[i] - locations[j]) <= k) {
186
+ f[i][k] = (f[i][k] + f[j][k - Math . abs(locations[i] - locations[j])]) % mod;
187
+ }
188
+ }
189
+ }
190
+ }
191
+ return f[start][fuel];
192
+ }
193
+ }
194
+ ```
195
+
149
196
### ** C++**
150
197
151
198
``` cpp
@@ -157,7 +204,7 @@ public:
157
204
memset(f, -1, sizeof(f));
158
205
const int mod = 1e9 + 7;
159
206
function<int(int, int)> dfs = [ &] (int i, int k) -> int {
160
- if (k < 0 || abs(locations[ i] - locations[ finish] ) > k ) {
207
+ if (k < abs(locations[ i] - locations[ finish] )) {
161
208
return 0;
162
209
}
163
210
if (f[ i] [ k ] != -1) {
@@ -176,6 +223,31 @@ public:
176
223
};
177
224
```
178
225
226
+ ```cpp
227
+ class Solution {
228
+ public:
229
+ int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
230
+ const int mod = 1e9 + 7;
231
+ int n = locations.size();
232
+ int f[n][fuel + 1];
233
+ memset(f, 0, sizeof(f));
234
+ for (int k = 0; k <= fuel; ++k) {
235
+ f[finish][k] = 1;
236
+ }
237
+ for (int k = 0; k <= fuel; ++k) {
238
+ for (int i = 0; i < n; ++i) {
239
+ for (int j = 0; j < n; ++j) {
240
+ if (j != i && abs(locations[i] - locations[j]) <= k) {
241
+ f[i][k] = (f[i][k] + f[j][k - abs(locations[i] - locations[j])]) % mod;
242
+ }
243
+ }
244
+ }
245
+ }
246
+ return f[start][fuel];
247
+ }
248
+ };
249
+ ```
250
+
179
251
### ** Go**
180
252
181
253
``` go
@@ -191,7 +263,7 @@ func countRoutes(locations []int, start int, finish int, fuel int) int {
191
263
const mod = 1e9 + 7
192
264
var dfs func (int , int ) int
193
265
dfs = func (i, k int ) (ans int ) {
194
- if k < 0 || abs(locations[i]-locations[finish]) > k {
266
+ if k < abs (locations[i]-locations[finish]) {
195
267
return 0
196
268
}
197
269
if f[i][k] != -1 {
@@ -219,6 +291,37 @@ func abs(x int) int {
219
291
}
220
292
```
221
293
294
+ ``` go
295
+ func countRoutes (locations []int , start int , finish int , fuel int ) int {
296
+ n := len (locations)
297
+ const mod = 1e9 + 7
298
+ f := make ([][]int , n)
299
+ for i := range f {
300
+ f[i] = make ([]int , fuel+1 )
301
+ }
302
+ for k := 0 ; k <= fuel; k++ {
303
+ f[finish][k] = 1
304
+ }
305
+ for k := 0 ; k <= fuel; k++ {
306
+ for i := 0 ; i < n; i++ {
307
+ for j := 0 ; j < n; j++ {
308
+ if j != i && abs (locations[i]-locations[j]) <= k {
309
+ f[i][k] = (f[i][k] + f[j][k-abs (locations[i]-locations[j])]) % mod
310
+ }
311
+ }
312
+ }
313
+ }
314
+ return f[start][fuel]
315
+ }
316
+
317
+ func abs (x int ) int {
318
+ if x < 0 {
319
+ return -x
320
+ }
321
+ return x
322
+ }
323
+ ```
324
+
222
325
### ** TypeScript**
223
326
224
327
``` ts
@@ -232,15 +335,15 @@ function countRoutes(
232
335
const f = Array .from ({ length: n }, () => Array (fuel + 1 ).fill (- 1 ));
233
336
const mod = 1e9 + 7 ;
234
337
const dfs = (i : number , k : number ): number => {
235
- if (k < 0 || Math .abs (locations [i ] - locations [finish ]) > k ) {
338
+ if (k < Math .abs (locations [i ] - locations [finish ])) {
236
339
return 0 ;
237
340
}
238
341
if (f [i ][k ] !== - 1 ) {
239
342
return f [i ][k ];
240
343
}
241
344
let ans = i === finish ? 1 : 0 ;
242
345
for (let j = 0 ; j < n ; ++ j ) {
243
- if (j != i ) {
346
+ if (j !== i ) {
244
347
const x = Math .abs (locations [i ] - locations [j ]);
245
348
ans = (ans + dfs (j , k - x )) % mod ;
246
349
}
@@ -251,6 +354,35 @@ function countRoutes(
251
354
}
252
355
```
253
356
357
+ ``` ts
358
+ function countRoutes(
359
+ locations : number [],
360
+ start : number ,
361
+ finish : number ,
362
+ fuel : number ,
363
+ ): number {
364
+ const n = locations .length ;
365
+ const f = Array .from ({ length: n }, () => Array (fuel + 1 ).fill (0 ));
366
+ for (let k = 0 ; k <= fuel ; ++ k ) {
367
+ f [finish ][k ] = 1 ;
368
+ }
369
+ const mod = 1e9 + 7 ;
370
+ for (let k = 0 ; k <= fuel ; ++ k ) {
371
+ for (let i = 0 ; i < n ; ++ i ) {
372
+ for (let j = 0 ; j < n ; ++ j ) {
373
+ if (j !== i && Math .abs (locations [i ] - locations [j ]) <= k ) {
374
+ f [i ][k ] =
375
+ (f [i ][k ] +
376
+ f [j ][k - Math .abs (locations [i ] - locations [j ])]) %
377
+ mod ;
378
+ }
379
+ }
380
+ }
381
+ }
382
+ return f [start ][fuel ];
383
+ }
384
+ ```
385
+
254
386
### ** ...**
255
387
256
388
```
0 commit comments