[fix][javascript] path-with-maximum-probability (#1587)

This commit is contained in:
Jayden
2024-08-29 01:39:59 +08:00
committed by GitHub
parent 1a5d6a477b
commit fc4b58bafc

View File

@ -49216,14 +49216,16 @@ function dijkstra(start, end, graph) {
distTo[start] = 1;
// 优先级队列distFromStart 较小的排在前面
const pq = new PriorityQueue((a, b) => {
return b.distFromStart - a.distFromStart;
const pq = new PriorityQueue({
compare: (a, b) => {
return b.distFromStart - a.distFromStart;
}
});
// 从起点 start 开始进行 BFS
pq.offer(new State(start, 1));
pq.enqueue(new State(start, 1));
while (!pq.isEmpty()) {
const curState = pq.poll();
const curState = pq.dequeue();
const curNodeID = curState.id;
const curDistFromStart = curState.distFromStart;
@ -49245,32 +49247,12 @@ function dijkstra(start, end, graph) {
// 更新 dp table
distTo[nextNodeID] = distToNextNode;
// 将这个节点以及距离放入队列
pq.offer(new State(nextNodeID, distToNextNode));
pq.enqueue(new State(nextNodeID, distToNextNode));
}
}
}
return 0.0;
}
class PriorityQueue {
constructor(compare) {
this.queue = [];
this.compare = compare;
}
offer(val) {
this.queue.push(val);
this.queue.sort(this.compare);
}
poll() {
return this.queue.shift();
}
isEmpty() {
return this.queue.length === 0;
}
}
```
```python