Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc4b58b

Browse files
[fix][javascript] path-with-maximum-probability (labuladong#1587)
1 parent 1a5d6a4 commit fc4b58b

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

‎多语言解法代码/solution_code.md

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49216,14 +49216,16 @@ function dijkstra(start, end, graph) {
4921649216
distTo[start] = 1;
4921749217

4921849218
// 优先级队列,distFromStart 较小的排在前面
49219-
const pq = new PriorityQueue((a, b) => {
49220-
return b.distFromStart - a.distFromStart;
49219+
const pq = new PriorityQueue({
49220+
compare: (a, b) => {
49221+
return b.distFromStart - a.distFromStart;
49222+
}
4922149223
});
4922249224
// 从起点 start 开始进行 BFS
49223-
pq.offer(new State(start, 1));
49225+
pq.enqueue(new State(start, 1));
4922449226

4922549227
while (!pq.isEmpty()) {
49226-
const curState = pq.poll();
49228+
const curState = pq.dequeue();
4922749229
const curNodeID = curState.id;
4922849230
const curDistFromStart = curState.distFromStart;
4922949231

@@ -49245,32 +49247,12 @@ function dijkstra(start, end, graph) {
4924549247
// 更新 dp table
4924649248
distTo[nextNodeID] = distToNextNode;
4924749249
// 将这个节点以及距离放入队列
49248-
pq.offer(new State(nextNodeID, distToNextNode));
49250+
pq.enqueue(new State(nextNodeID, distToNextNode));
4924949251
}
4925049252
}
4925149253
}
4925249254
return 0.0;
4925349255
}
49254-
49255-
class PriorityQueue {
49256-
constructor(compare) {
49257-
this.queue = [];
49258-
this.compare = compare;
49259-
}
49260-
49261-
offer(val) {
49262-
this.queue.push(val);
49263-
this.queue.sort(this.compare);
49264-
}
49265-
49266-
poll() {
49267-
return this.queue.shift();
49268-
}
49269-
49270-
isEmpty() {
49271-
return this.queue.length === 0;
49272-
}
49273-
}
4927449256
```
4927549257

4927649258
```python

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /