Skip to content

Commit bb55fa9

Browse files
committed
Update findTheCity.ts
1 parent 58cb3bc commit bb55fa9

File tree

1 file changed

+52
-52
lines changed
  • find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance

1 file changed

+52
-52
lines changed
Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
import { BinaryHeap } from "https://deno.land/std@0.165.0/collections/binary_heap.ts";
2-
export default function findTheCity(
3-
n: number,
4-
edges: number[][],
5-
distanceThreshold: number
6-
): number {
7-
const dc: [number, number][][] = new Array(n).fill(0).map(() => []);
8-
const has: number[][] = Array(n)
9-
.fill(0)
10-
.map(() => Array(n).fill(distanceThreshold + 1));
11-
const heap = new BinaryHeap<[number, number, number]>(
12-
(a, b) => a[0] - b[0]
13-
);
14-
15-
for (const [from, to, weight] of edges) {
16-
if (weight <= distanceThreshold) {
17-
dc[from].push([to, weight]);
18-
dc[to].push([from, weight]);
19-
heap.push([weight, from, to]);
20-
has[from][to] = has[to][from] = weight;
21-
}
22-
}
23-
for (let i = 0; i < n; i++) has[i][i] = 0;
24-
25-
while (!heap.isEmpty()) {
26-
const [x, a, b] = heap.pop() as [number, number, number];
27-
28-
loop(b, x, a);
29-
loop(a, x, b);
30-
}
31-
let minx = -1,
32-
mnum = n + 1;
33-
34-
for (let x = n - 1; x >= 0; x--) {
35-
const tep = has[x].filter((i) => i <= distanceThreshold).length;
36-
if (tep < mnum) {
37-
minx = x;
38-
mnum = tep;
39-
}
40-
}
41-
return minx;
42-
43-
function loop(b: number, x: number, a: number) {
44-
for (const [i, j] of dc[b]) {
45-
const r = j + x;
46-
if (r < has[i][a]) {
47-
heap.push([r, i, a]);
48-
has[i][a] = has[a][i] = r;
49-
}
50-
}
51-
}
52-
}
1+
import { BinaryHeap } from "https://deno.land/std@0.165.0/collections/binary_heap.ts";
2+
export default function findTheCity(
3+
n: number,
4+
edges: number[][],
5+
distanceThreshold: number,
6+
): number {
7+
const dc: [number, number][][] = new Array(n).fill(0).map(() => []);
8+
const has: number[][] = Array(n)
9+
.fill(0)
10+
.map(() => Array(n).fill(distanceThreshold + 1));
11+
const heap = new BinaryHeap<[number, number, number]>(
12+
(a, b) => a[0] - b[0],
13+
);
14+
15+
for (const [from, to, weight] of edges) {
16+
if (weight <= distanceThreshold) {
17+
dc[from].push([to, weight]);
18+
dc[to].push([from, weight]);
19+
heap.push([weight, from, to]);
20+
has[from][to] = has[to][from] = weight;
21+
}
22+
}
23+
for (let i = 0; i < n; i++) has[i][i] = 0;
24+
25+
while (!heap.isEmpty()) {
26+
const [x, a, b] = heap.pop() as [number, number, number];
27+
28+
loop(b, x, a);
29+
loop(a, x, b);
30+
}
31+
let minx = -1,
32+
mnum = n + 1;
33+
34+
for (let x = n - 1; x >= 0; x--) {
35+
const tep = has[x].filter((i) => i <= distanceThreshold).length;
36+
if (tep < mnum) {
37+
minx = x;
38+
mnum = tep;
39+
}
40+
}
41+
return minx;
42+
43+
function loop(b: number, x: number, a: number) {
44+
for (const [i, j] of dc[b]) {
45+
const r = j + x;
46+
if (r < has[i][a]) {
47+
heap.push([r, i, a]);
48+
has[i][a] = has[a][i] = r;
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)