Skip to content

Commit fd0f57a

Browse files
authored
Update 743-network-delay-time.js
1 parent 099dbff commit fd0f57a

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

743-network-delay-time.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,104 @@ const networkDelayTime = function(times, N, K) {
4747
const res = Math.max(...distances);
4848
return res === Infinity ? -1 : res;
4949
};
50+
51+
// another
52+
53+
/**
54+
* @param {number[][]} times
55+
* @param {number} N
56+
* @param {number} K
57+
* @return {number}
58+
*/
59+
const networkDelayTime = function (times, N, K) {
60+
const hash = {}
61+
for(const [u, v, t] of times) {
62+
if(hash[u] == null) hash[u] = {}
63+
hash[u][v] = t
64+
}
65+
const pq = new PriorityQueue((a, b) => a[0] < b[0])
66+
pq.push([0, K])
67+
const visited = Array.from(N + 1)
68+
let res = 0
69+
while(!pq.isEmpty()) {
70+
const [dist, cur] = pq.pop()
71+
if(visited[cur]) continue
72+
visited[cur] = true
73+
res = dist
74+
N--
75+
if(hash[cur]) {
76+
for(let next of Object.keys(hash[cur])) {
77+
pq.push([dist + hash[cur][next], next])
78+
}
79+
}
80+
}
81+
return N === 0 ? res : -1
82+
}
83+
84+
class PriorityQueue {
85+
constructor(comparator = (a, b) => a > b) {
86+
this.heap = []
87+
this.top = 0
88+
this.comparator = comparator
89+
}
90+
size() {
91+
return this.heap.length
92+
}
93+
isEmpty() {
94+
return this.size() === 0
95+
}
96+
peek() {
97+
return this.heap[this.top]
98+
}
99+
push(...values) {
100+
values.forEach((value) => {
101+
this.heap.push(value)
102+
this.siftUp()
103+
})
104+
return this.size()
105+
}
106+
pop() {
107+
const poppedValue = this.peek()
108+
const bottom = this.size() - 1
109+
if (bottom > this.top) {
110+
this.swap(this.top, bottom)
111+
}
112+
this.heap.pop()
113+
this.siftDown()
114+
return poppedValue
115+
}
116+
replace(value) {
117+
const replacedValue = this.peek()
118+
this.heap[this.top] = value
119+
this.siftDown()
120+
return replacedValue
121+
}
122+
123+
parent = (i) => ((i + 1) >>> 1) - 1
124+
left = (i) => (i << 1) + 1
125+
right = (i) => (i + 1) << 1
126+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
127+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
128+
siftUp = () => {
129+
let node = this.size() - 1
130+
while (node > this.top && this.greater(node, this.parent(node))) {
131+
this.swap(node, this.parent(node))
132+
node = this.parent(node)
133+
}
134+
}
135+
siftDown = () => {
136+
let node = this.top
137+
while (
138+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
139+
(this.right(node) < this.size() && this.greater(this.right(node), node))
140+
) {
141+
let maxChild =
142+
this.right(node) < this.size() &&
143+
this.greater(this.right(node), this.left(node))
144+
? this.right(node)
145+
: this.left(node)
146+
this.swap(node, maxChild)
147+
node = maxChild
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)