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 74039cc

Browse files
Update 2045-second-minimum-time-to-reach-destination.js
1 parent 2151d63 commit 74039cc

File tree

1 file changed

+85
-18
lines changed

1 file changed

+85
-18
lines changed

‎2045-second-minimum-time-to-reach-destination.js

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
1-
const initializeGraph = (n) => {
2-
let G = []
3-
for (let i = 0; i < n; i++) {
4-
G.push([])
5-
}
6-
return G
7-
}
8-
const addEdgeToG = (G, Edges) => {
9-
for (const [u, v] of Edges) {
10-
G[u].push(v)
11-
G[v].push(u)
12-
}
13-
}
141
/**
152
* @param {number} n
163
* @param {number[][]} edges
174
* @param {number} time
185
* @param {number} change
196
* @return {number}
207
*/
21-
const secondMinimum = (n, edges, time, change)=> {
8+
var secondMinimum = function(n, edges, time, change) {
229
let adj = initializeGraph(n + 1)
2310
addEdgeToG(adj, edges)
2411
let cost = initializeGraph(n + 1)
25-
let pq = new MinPriorityQueue({priority: (x) => x[0] })
26-
pq.enqueue([0, 1])
12+
let pq = new PQ((a,b) => a[0] <b[0])
13+
pq.push([0, 1])
2714
let green = 2 * change
2815
while (pq.size()) {
29-
let cur = pq.dequeue().element
16+
let cur = pq.pop()
3017
let [t, node] = cur
3118
if (cost[node].length == 2) continue
3219
let nextT =
@@ -45,9 +32,89 @@ const secondMinimum = (n, edges, time, change) => {
4532
continue
4633
}
4734
}
48-
for (const next_node of adj[node]) pq.enqueue([nextT + time, next_node])
35+
for (const next_node of adj[node]) pq.push([nextT + time, next_node])
4936
}
5037
return cost[n][1]
38+
};
39+
function initializeGraph(n) {
40+
let G = []
41+
for (let i = 0; i < n; i++) {
42+
G.push([])
43+
}
44+
return G
45+
}
46+
function addEdgeToG(G, Edges) {
47+
for (const [u, v] of Edges) {
48+
G[u].push(v)
49+
G[v].push(u)
50+
}
51+
}
52+
class PQ {
53+
constructor(comparator = (a, b) => a > b) {
54+
this.heap = []
55+
this.top = 0
56+
this.comparator = comparator
57+
}
58+
size() {
59+
return this.heap.length
60+
}
61+
isEmpty() {
62+
return this.size() === 0
63+
}
64+
peek() {
65+
return this.heap[this.top]
66+
}
67+
push(...values) {
68+
values.forEach((value) => {
69+
this.heap.push(value)
70+
this.siftUp()
71+
})
72+
return this.size()
73+
}
74+
pop() {
75+
const poppedValue = this.peek()
76+
const bottom = this.size() - 1
77+
if (bottom > this.top) {
78+
this.swap(this.top, bottom)
79+
}
80+
this.heap.pop()
81+
this.siftDown()
82+
return poppedValue
83+
}
84+
replace(value) {
85+
const replacedValue = this.peek()
86+
this.heap[this.top] = value
87+
this.siftDown()
88+
return replacedValue
89+
}
90+
91+
parent = (i) => ((i + 1) >>> 1) - 1
92+
left = (i) => (i << 1) + 1
93+
right = (i) => (i + 1) << 1
94+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
95+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
96+
siftUp = () => {
97+
let node = this.size() - 1
98+
while (node > this.top && this.greater(node, this.parent(node))) {
99+
this.swap(node, this.parent(node))
100+
node = this.parent(node)
101+
}
102+
}
103+
siftDown = () => {
104+
let node = this.top
105+
while (
106+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
107+
(this.right(node) < this.size() && this.greater(this.right(node), node))
108+
) {
109+
let maxChild =
110+
this.right(node) < this.size() &&
111+
this.greater(this.right(node), this.left(node))
112+
? this.right(node)
113+
: this.left(node)
114+
this.swap(node, maxChild)
115+
node = maxChild
116+
}
117+
}
51118
}
52119

53120
// another

0 commit comments

Comments
(0)

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