Skip to content

Commit b9e5cff

Browse files
committed
feat: add solutions to lc problem: No.1575
No.1575.Count All Possible Routes
1 parent 4bd1fbb commit b9e5cff

File tree

7 files changed

+321
-44
lines changed

7 files changed

+321
-44
lines changed

solution/1500-1599/1575.Count All Possible Routes/README.md

Lines changed: 149 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,26 @@
7272

7373
函数 $dfs(i, k)$ 的计算过程如下:
7474

75-
- 如果 $k \lt 0$ 或者 $|locations[i] - locations[finish]| \gt k$,那么返回 $0$。
75+
- 如果 $k \lt |locations[i] - locations[finish]|$,那么返回 $0$。
7676
- 如果 $i = finish$,那么答案路径数初始时为 $1$,否则为 $0$。
7777
- 然后,我们遍历所有城市 $j$,如果 $j \ne i$,那么我们可以从城市 $i$ 移动到城市 $j$,此时剩余汽油量为 $k - |locations[i] - locations[j]|$,那么我们可以将答案路径数加上 $dfs(j, k - |locations[i] - locations[j]|)$。
7878
- 最后,我们返回答案路径数。
7979

8080
为了避免重复计算,我们可以使用记忆化搜索。
8181

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$ 的大小。
8395

8496
<!-- tabs:start -->
8597

@@ -89,25 +101,37 @@
89101

90102
```python
91103
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:
95105
@cache
96106
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]):
98108
return 0
99109
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
106114

107115
mod = 10**9 + 7
108116
return dfs(start, fuel)
109117
```
110118

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+
111135
### **Java**
112136

113137
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -129,7 +153,7 @@ class Solution {
129153
}
130154

131155
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])) {
133157
return 0;
134158
}
135159
if (f[i][k] != null) {
@@ -146,6 +170,29 @@ class Solution {
146170
}
147171
```
148172

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+
149196
### **C++**
150197

151198
```cpp
@@ -157,7 +204,7 @@ public:
157204
memset(f, -1, sizeof(f));
158205
const int mod = 1e9 + 7;
159206
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])) {
161208
return 0;
162209
}
163210
if (f[i][k] != -1) {
@@ -176,6 +223,31 @@ public:
176223
};
177224
```
178225
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+
179251
### **Go**
180252

181253
```go
@@ -191,7 +263,7 @@ func countRoutes(locations []int, start int, finish int, fuel int) int {
191263
const mod = 1e9 + 7
192264
var dfs func(int, int) int
193265
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]) {
195267
return 0
196268
}
197269
if f[i][k] != -1 {
@@ -219,6 +291,37 @@ func abs(x int) int {
219291
}
220292
```
221293

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+
222325
### **TypeScript**
223326

224327
```ts
@@ -232,15 +335,15 @@ function countRoutes(
232335
const f = Array.from({ length: n }, () => Array(fuel + 1).fill(-1));
233336
const mod = 1e9 + 7;
234337
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])) {
236339
return 0;
237340
}
238341
if (f[i][k] !== -1) {
239342
return f[i][k];
240343
}
241344
let ans = i === finish ? 1 : 0;
242345
for (let j = 0; j < n; ++j) {
243-
if (j != i) {
346+
if (j !== i) {
244347
const x = Math.abs(locations[i] - locations[j]);
245348
ans = (ans + dfs(j, k - x)) % mod;
246349
}
@@ -251,6 +354,35 @@ function countRoutes(
251354
}
252355
```
253356

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+
254386
### **...**
255387

256388
```

0 commit comments

Comments
 (0)