Skip to content

Commit e05f3f6

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

File tree

8 files changed

+274
-194
lines changed

8 files changed

+274
-194
lines changed

solution/1000-1099/1032.Stream of Characters/Solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def search(self, w: List[str]) -> bool:
2525

2626

2727
class StreamChecker:
28-
2928
def __init__(self, words: List[str]):
3029
self.trie = Trie()
3130
self.cs = []
@@ -35,7 +34,8 @@ def __init__(self, words: List[str]):
3534

3635
def query(self, letter: str) -> bool:
3736
self.cs.append(letter)
38-
return self.trie.search(self.cs[-self.limit:])
37+
return self.trie.search(self.cs[-self.limit :])
38+
3939

4040
# Your StreamChecker object will be instantiated and called as such:
4141
# obj = StreamChecker(words)

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

Lines changed: 101 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@
6868

6969
**方法一:记忆化搜索**
7070

71+
我们设计一个函数 $dfs(i, k)$,表示从城市 $i$ 出发,剩余汽油量为 $k$ 时,到达目的地 $finish$ 的路径数。那么答案就是 $dfs(start, fuel)$。
72+
73+
函数 $dfs(i, k)$ 的计算过程如下:
74+
75+
- 如果 $k \lt 0$ 或者 $|locations[i] - locations[finish]| \gt k$,那么返回 $0$。
76+
- 如果 $i = finish$,那么答案路径数初始时为 $1$,否则为 $0$。
77+
- 然后,我们遍历所有城市 $j$,如果 $j \ne i$,那么我们可以从城市 $i$ 移动到城市 $j$,此时剩余汽油量为 $k - |locations[i] - locations[j]|$,那么我们可以将答案路径数加上 $dfs(j, k - |locations[i] - locations[j]|)$。
78+
- 最后,我们返回答案路径数。
79+
80+
为了避免重复计算,我们可以使用记忆化搜索。
81+
82+
时间复杂度 $O(n^2 \times fuel)$,空间复杂度 $O(n \times fuel)$。其中 $n$ 为城市数量。
83+
7184
<!-- tabs:start -->
7285

7386
### **Python3**
@@ -80,15 +93,16 @@ class Solution:
8093
self, locations: List[int], start: int, finish: int, fuel: int
8194
) -> int:
8295
@cache
83-
def dfs(i, t):
84-
if abs(locations[i] - locations[finish]) > t:
96+
def dfs(i: int, k: int) -> int:
97+
if k < 0 or abs(locations[i] - locations[finish] > k):
8598
return 0
86-
res = int(i == finish)
87-
for j, v in enumerate(locations):
88-
if j != i:
89-
if (cost := abs(locations[i] - v)) <= t:
90-
res += dfs(j, t - cost)
91-
return res % mod
99+
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
92106

93107
mod = 10**9 + 7
94108
return dfs(start, fuel)
@@ -100,41 +114,34 @@ class Solution:
100114

101115
```java
102116
class Solution {
103-
private int[][] f;
104117
private int[] locations;
105-
private int target;
106-
private static final int MOD = (int) 1e9 + 7;
118+
private int finish;
119+
private int n;
120+
private Integer[][] f;
121+
private final int mod = (int) 1e9 + 7;
107122

108123
public int countRoutes(int[] locations, int start, int finish, int fuel) {
109-
int n = locations.length;
110-
f = new int[n + 1][fuel + 1];
124+
n = locations.length;
111125
this.locations = locations;
112-
target = finish;
113-
for (int i = 0; i < f.length; ++i) {
114-
Arrays.fill(f[i], -1);
115-
}
126+
this.finish = finish;
127+
f = new Integer[n][fuel + 1];
116128
return dfs(start, fuel);
117129
}
118130

119-
private int dfs(int i, int t) {
120-
if (f[i][t] != -1) {
121-
return f[i][t];
122-
}
123-
if (Math.abs(locations[i] - locations[target]) > t) {
131+
private int dfs(int i, int k) {
132+
if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) {
124133
return 0;
125134
}
126-
int res = i == target ? 1 : 0;
127-
for (int j = 0; j < locations.length; ++j) {
135+
if (f[i][k] != null) {
136+
return f[i][k];
137+
}
138+
int ans = i == finish ? 1 : 0;
139+
for (int j = 0; j < n; ++j) {
128140
if (j != i) {
129-
int cost = Math.abs(locations[i] - locations[j]);
130-
if (cost <= t) {
131-
res += dfs(j, t - cost);
132-
res %= MOD;
133-
}
141+
ans = (ans + dfs(j, k - Math.abs(locations[i] - locations[j]))) % mod;
134142
}
135143
}
136-
f[i][t] = res;
137-
return res;
144+
return f[i][k] = ans;
138145
}
139146
}
140147
```
@@ -144,25 +151,27 @@ class Solution {
144151
```cpp
145152
class Solution {
146153
public:
147-
const int mod = 1e9 + 7;
148-
149154
int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
150155
int n = locations.size();
151-
vector<vector<int>> f(n + 1, vector<int>(fuel + 1, -1));
152-
return dfs(start, fuel, locations, finish, f);
153-
}
154-
155-
int dfs(int i, int t, vector<int>& locations, int target, vector<vector<int>>& f) {
156-
if (f[i][t] != -1) return f[i][t];
157-
if (abs(locations[i] - locations[target]) > t) return 0;
158-
int res = i == target;
159-
for (int j = 0; j < locations.size(); ++j) {
160-
if (j == i) continue;
161-
int cost = abs(locations[i] - locations[j]);
162-
if (cost <= t) res = (res + dfs(j, t - cost, locations, target, f)) % mod;
163-
}
164-
f[i][t] = res;
165-
return res;
156+
int f[n][fuel + 1];
157+
memset(f, -1, sizeof(f));
158+
const int mod = 1e9 + 7;
159+
function<int(int, int)> dfs = [&](int i, int k) -> int {
160+
if (k < 0 || abs(locations[i] - locations[finish]) > k) {
161+
return 0;
162+
}
163+
if (f[i][k] != -1) {
164+
return f[i][k];
165+
}
166+
int ans = i == finish;
167+
for (int j = 0; j < n; ++j) {
168+
if (j != i) {
169+
ans = (ans + dfs(j, k - abs(locations[i] - locations[j]))) % mod;
170+
}
171+
}
172+
return f[i][k] = ans;
173+
};
174+
return dfs(start, fuel);
166175
}
167176
};
168177
```
@@ -172,36 +181,32 @@ public:
172181
```go
173182
func countRoutes(locations []int, start int, finish int, fuel int) int {
174183
n := len(locations)
175-
f := make([][]int, n+1)
184+
f := make([][]int, n)
176185
for i := range f {
177186
f[i] = make([]int, fuel+1)
178187
for j := range f[i] {
179188
f[i][j] = -1
180189
}
181190
}
182-
mod := int(1e9) + 7
191+
const mod = 1e9 + 7
183192
var dfs func(int, int) int
184-
dfs = func(i, t int) int {
185-
if f[i][t] != -1 {
186-
return f[i][t]
187-
}
188-
if abs(locations[i]-locations[finish]) > t {
193+
dfs = func(i, k int) (ans int) {
194+
if k < 0 || abs(locations[i]-locations[finish]) > k {
189195
return 0
190196
}
191-
res := 0
197+
if f[i][k] != -1 {
198+
return f[i][k]
199+
}
192200
if i == finish {
193-
res++
201+
ans = 1
194202
}
195-
for j, v := range locations {
203+
for j, x := range locations {
196204
if j != i {
197-
cost := abs(locations[i] - v)
198-
if cost <= t {
199-
res = (res + dfs(j, t-cost)) % mod
200-
}
205+
ans = (ans + dfs(j, k-abs(locations[i]-x))) % mod
201206
}
202207
}
203-
f[i][t] = res
204-
return res
208+
f[i][k] = ans
209+
return
205210
}
206211
return dfs(start, fuel)
207212
}
@@ -214,6 +219,38 @@ func abs(x int) int {
214219
}
215220
```
216221

222+
### **TypeScript**
223+
224+
```ts
225+
function countRoutes(
226+
locations: number[],
227+
start: number,
228+
finish: number,
229+
fuel: number,
230+
): number {
231+
const n = locations.length;
232+
const f = Array.from({ length: n }, () => Array(fuel + 1).fill(-1));
233+
const mod = 1e9 + 7;
234+
const dfs = (i: number, k: number): number => {
235+
if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) {
236+
return 0;
237+
}
238+
if (f[i][k] !== -1) {
239+
return f[i][k];
240+
}
241+
let ans = i === finish ? 1 : 0;
242+
for (let j = 0; j < n; ++j) {
243+
if (j != i) {
244+
const x = Math.abs(locations[i] - locations[j]);
245+
ans = (ans + dfs(j, k - x)) % mod;
246+
}
247+
}
248+
return (f[i][k] = ans);
249+
};
250+
return dfs(start, fuel);
251+
}
252+
```
253+
217254
### **...**
218255

219256
```

0 commit comments

Comments
 (0)