1
var maxProbability = function (n, edges, succProb, start, end) {
2
const graph = new Map();
3
edges.forEach(([a, b], i) => {
4
const aSet = graph.get(a) || [];
5
const bSet = graph.get(b) || [];
6
aSet.push([b, succProb[i]]), bSet.push([a, succProb[i]]);
7
graph.set(a, aSet), graph.set(b, bSet);
8
});
9

10
const dist = new Array(n).fill(0);
11
const vis = new Array(n).fill(false);
12

13
dist[start] = 1;
14

15
const getMaxProbNode = () => {
16
let maxVal = 0,
17
maxIndex = -1;
18
for (let i = 0; i < n; i++) {
19
if (maxVal < dist[i] && !vis[i]) {
20
(maxVal = dist[i]), (maxIndex = i);
21
}
22
}
23
return maxIndex;
24
};
25

26
for (let i = 0; i < n - 1; i++) {
27
const maxProbNode = getMaxProbNode();
28
vis[maxProbNode] = true;
29

30
const adjacentNodes = graph.get(maxProbNode) || [];
31
const len = adjacentNodes.length;
32
for (let j = 0; j < len; j++) {
33
const [node, prob] = adjacentNodes[j];
34
if (!vis[node] && dist[node] < dist[maxProbNode] * prob) {
35
dist[node] = dist[maxProbNode] * prob;
36
}
37
}
38
}
39

40
return dist[end];
41
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0