Skip to content

Commit 713fd3e

Browse files
committed
feat: add js solution to lc problem: No. 1334
1 parent 98a4c74 commit 713fd3e

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,59 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
585585
}
586586
```
587587

588+
#### JavaScript
589+
590+
```js
591+
export function findTheCity(n, edges, distanceThreshold) {
592+
const MAX = Number.POSITIVE_INFINITY;
593+
const g = Array.from({ length: n }, () => new Map());
594+
const dist = Array(n).fill(MAX);
595+
const vis = Array(n).fill(false);
596+
for (const [f, t, w] of edges) {
597+
g[f].set(t, w);
598+
g[t].set(f, w);
599+
}
600+
601+
const dijkstra = u => {
602+
dist.fill(MAX);
603+
vis.fill(false);
604+
dist[u] = 0;
605+
const pq = new MinPriorityQueue();
606+
pq.enqueue(u, 0);
607+
608+
while (!pq.isEmpty()) {
609+
const u = pq.dequeue().element;
610+
if (vis[u]) continue;
611+
vis[u] = true;
612+
613+
for (const [v, w] of g[u]) {
614+
if (vis[v]) continue;
615+
616+
const wNext = dist[u] + w;
617+
if (wNext < dist[v]) {
618+
dist[v] = wNext;
619+
pq.enqueue(v, dist[v]);
620+
}
621+
}
622+
}
623+
624+
return dist.filter(d => d <= distanceThreshold).length;
625+
};
626+
627+
let ans = n;
628+
let cnt = MAX;
629+
for (let i = n - 1; i >= 0; --i) {
630+
const t = dijkstra(i);
631+
if (t < cnt) {
632+
cnt = t;
633+
ans = i;
634+
}
635+
}
636+
637+
return ans;
638+
}
639+
```
640+
588641
<!-- tabs:end -->
589642

590643
<!-- solution:end -->

solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,59 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
567567
}
568568
```
569569

570+
#### JavaScript
571+
572+
```js
573+
export function findTheCity(n, edges, distanceThreshold) {
574+
const MAX = Number.POSITIVE_INFINITY;
575+
const g = Array.from({ length: n }, () => new Map());
576+
const dist = Array(n).fill(MAX);
577+
const vis = Array(n).fill(false);
578+
for (const [f, t, w] of edges) {
579+
g[f].set(t, w);
580+
g[t].set(f, w);
581+
}
582+
583+
const dijkstra = u => {
584+
dist.fill(MAX);
585+
vis.fill(false);
586+
dist[u] = 0;
587+
const pq = new MinPriorityQueue();
588+
pq.enqueue(u, 0);
589+
590+
while (!pq.isEmpty()) {
591+
const u = pq.dequeue().element;
592+
if (vis[u]) continue;
593+
vis[u] = true;
594+
595+
for (const [v, w] of g[u]) {
596+
if (vis[v]) continue;
597+
598+
const wNext = dist[u] + w;
599+
if (wNext < dist[v]) {
600+
dist[v] = wNext;
601+
pq.enqueue(v, dist[v]);
602+
}
603+
}
604+
}
605+
606+
return dist.filter(d => d <= distanceThreshold).length;
607+
};
608+
609+
let ans = n;
610+
let cnt = MAX;
611+
for (let i = n - 1; i >= 0; --i) {
612+
const t = dijkstra(i);
613+
if (t < cnt) {
614+
cnt = t;
615+
ans = i;
616+
}
617+
}
618+
619+
return ans;
620+
}
621+
```
622+
570623
<!-- tabs:end -->
571624

572625
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export function findTheCity(n, edges, distanceThreshold) {
2+
const MAX = Number.POSITIVE_INFINITY;
3+
const g = Array.from({ length: n }, () => new Map());
4+
const dist = Array(n).fill(MAX);
5+
const vis = Array(n).fill(false);
6+
for (const [f, t, w] of edges) {
7+
g[f].set(t, w);
8+
g[t].set(f, w);
9+
}
10+
11+
const dijkstra = u => {
12+
dist.fill(MAX);
13+
vis.fill(false);
14+
dist[u] = 0;
15+
const pq = new MinPriorityQueue();
16+
pq.enqueue(u, 0);
17+
18+
while (!pq.isEmpty()) {
19+
const u = pq.dequeue().element;
20+
if (vis[u]) continue;
21+
vis[u] = true;
22+
23+
for (const [v, w] of g[u]) {
24+
if (vis[v]) continue;
25+
26+
const wNext = dist[u] + w;
27+
if (wNext < dist[v]) {
28+
dist[v] = wNext;
29+
pq.enqueue(v, dist[v]);
30+
}
31+
}
32+
}
33+
34+
return dist.filter(d => d <= distanceThreshold).length;
35+
};
36+
37+
let ans = n;
38+
let cnt = MAX;
39+
for (let i = n - 1; i >= 0; --i) {
40+
const t = dijkstra(i);
41+
if (t < cnt) {
42+
cnt = t;
43+
ans = i;
44+
}
45+
}
46+
47+
return ans;
48+
}

0 commit comments

Comments
 (0)