Skip to content

Commit 71f3195

Browse files
authored
Create 1514-path-with-maximum-probability.js
1 parent 4888acc commit 71f3195

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

1514-path-with-maximum-probability.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @param {number[]} succProb
5+
* @param {number} start
6+
* @param {number} end
7+
* @return {number}
8+
*/
9+
var maxProbability = function (n, edges, succProb, start, end) {
10+
const g = {}
11+
for (let i = 0; i < edges.length; ++i) {
12+
const a = edges[i][0],
13+
b = edges[i][1]
14+
if (g[a] == null) g[a] = []
15+
if (g[b] == null) g[b] = []
16+
g[a].push([b, i])
17+
g[b].push([a, i])
18+
}
19+
const p = new Array(n).fill(0)
20+
p[start] = 1
21+
const pq = new PriorityQueue((a, b) => -p[a] < -p[b])
22+
pq.push(start)
23+
while (!pq.isEmpty()) {
24+
const cur = pq.pop()
25+
if (cur === end) {
26+
return p[end]
27+
}
28+
for (let a of g[cur] || []) {
29+
const neighbor = a[0],
30+
index = a[1]
31+
if (p[cur] * succProb[index] > p[neighbor]) {
32+
p[neighbor] = p[cur] * succProb[index]
33+
pq.push(neighbor)
34+
}
35+
}
36+
}
37+
return 0
38+
}
39+
class PriorityQueue {
40+
constructor(comparator = (a, b) => a > b) {
41+
this.heap = []
42+
this.top = 0
43+
this.comparator = comparator
44+
}
45+
size() {
46+
return this.heap.length
47+
}
48+
isEmpty() {
49+
return this.size() === 0
50+
}
51+
peek() {
52+
return this.heap[this.top]
53+
}
54+
push(...values) {
55+
values.forEach((value) => {
56+
this.heap.push(value)
57+
this.siftUp()
58+
})
59+
return this.size()
60+
}
61+
pop() {
62+
const poppedValue = this.peek()
63+
const bottom = this.size() - 1
64+
if (bottom > this.top) {
65+
this.swap(this.top, bottom)
66+
}
67+
this.heap.pop()
68+
this.siftDown()
69+
return poppedValue
70+
}
71+
replace(value) {
72+
const replacedValue = this.peek()
73+
this.heap[this.top] = value
74+
this.siftDown()
75+
return replacedValue
76+
}
77+
78+
parent = (i) => ((i + 1) >>> 1) - 1
79+
left = (i) => (i << 1) + 1
80+
right = (i) => (i + 1) << 1
81+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
82+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
83+
siftUp = () => {
84+
let node = this.size() - 1
85+
while (node > this.top && this.greater(node, this.parent(node))) {
86+
this.swap(node, this.parent(node))
87+
node = this.parent(node)
88+
}
89+
}
90+
siftDown = () => {
91+
let node = this.top
92+
while (
93+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
94+
(this.right(node) < this.size() && this.greater(this.right(node), node))
95+
) {
96+
let maxChild =
97+
this.right(node) < this.size() &&
98+
this.greater(this.right(node), this.left(node))
99+
? this.right(node)
100+
: this.left(node)
101+
this.swap(node, maxChild)
102+
node = maxChild
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)