-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
52 lines (47 loc) · 1.26 KB
/
solution1.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* https://leetcode-cn.com/problems/frog-position-after-t-seconds/
*
* 1377. T 秒后青蛙的位置
*
* Hard
*
* 76ms 87.80%
* 37.9mb 100.00%
*/
const frogPosition = (n, edges, t, target) => {
if (!edges.length) {
return 1.0;
}
const record = new Map();
for (let i = 0; i < edges.length; i++) {
if (!record.has(edges[i][0])) {
record.set(edges[i][0], []);
}
// 避免重新记录
if (edges[i][1] > edges[i][0]) {
record.get(edges[i][0]).push(edges[i][1]);
}
if (!record.has(edges[i][1])) {
record.set(edges[i][1], []);
}
if (edges[i][0] > edges[i][1]) {
record.get(edges[i][1]).push(edges[i][0]);
}
}
let ans = 0;
dfs(1, record, t, target, 1);
return ans;
function dfs(startIndex, record, t, target, probability) {
const indexsList = record.get(startIndex);
if (target === startIndex && ( t === 0 || (t > 0 && !indexsList.length))) {
ans = probability;
return
}
if (t === 0 || !indexsList.length) {
return
}
for (let i = 0, max = indexsList.length; i < max; i++) {
dfs(indexsList[i], record, t - 1, target, probability * (1 / max));
}
}
}