Skip to content

Commit e3b21c3

Browse files
authored
Update 1514-path-with-maximum-probability.js
1 parent 89ccb83 commit e3b21c3

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

1514-path-with-maximum-probability.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @param {number} end
77
* @return {number}
88
*/
9-
const maxProbability = function (n, edges, succProb, start, end) {
9+
var maxProbability = function (n, edges, succProb, start, end) {
1010
const g = {}
1111
for (let i = 0; i < edges.length; ++i) {
1212
const a = edges[i][0],
@@ -16,15 +16,13 @@ const maxProbability = function (n, edges, succProb, start, end) {
1616
g[a].push([b, i])
1717
g[b].push([a, i])
1818
}
19-
const p = new Array(n).fill(0)
19+
const p = new Array(n).fill(-1)
2020
p[start] = 1
21-
const pq = new PriorityQueue((a, b) => p[a] > p[b])
21+
const pq = new PQ((a, b) => p[a] > p[b])
2222
pq.push(start)
2323
while (!pq.isEmpty()) {
2424
const cur = pq.pop()
25-
if (cur === end) {
26-
return p[end]
27-
}
25+
2826
for (let a of g[cur] || []) {
2927
const neighbor = a[0],
3028
index = a[1]
@@ -34,9 +32,9 @@ const maxProbability = function (n, edges, succProb, start, end) {
3432
}
3533
}
3634
}
37-
return 0
35+
return p[end] === -1 ? 0 : p[end]
3836
}
39-
class PriorityQueue {
37+
class PQ {
4038
constructor(comparator = (a, b) => a > b) {
4139
this.heap = []
4240
this.top = 0

0 commit comments

Comments
 (0)