Skip to content

Commit 98a4c74

Browse files
committed
feat: add ts solution to lc problem: No.1334
1 parent aa4e2e0 commit 98a4c74

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,4 +526,67 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
526526

527527
<!-- solution:end -->
528528

529+
<!-- solution:start -->
530+
531+
### Solution 3
532+
533+
<!-- tabs:start -->
534+
535+
#### TypeScript
536+
537+
```ts
538+
function findTheCity(n: number, edges: number[][], distanceThreshold: number): number {
539+
const MAX = Number.POSITIVE_INFINITY;
540+
const g = Array.from({ length: n }, () => new Map<number, number>());
541+
const dist: number[] = Array(n).fill(MAX);
542+
const vis: boolean[] = Array(n).fill(false);
543+
for (const [f, t, w] of edges) {
544+
g[f].set(t, w);
545+
g[t].set(f, w);
546+
}
547+
548+
const dijkstra = (u: number): number => {
549+
dist.fill(MAX);
550+
vis.fill(false);
551+
dist[u] = 0;
552+
const pq = new MinPriorityQueue();
553+
pq.enqueue(u, 0);
554+
555+
while (!pq.isEmpty()) {
556+
const u = pq.dequeue().element;
557+
if (vis[u]) continue;
558+
vis[u] = true;
559+
560+
for (const [v, w] of g[u]) {
561+
if (vis[v]) continue;
562+
563+
const wNext = dist[u] + w;
564+
if (wNext < dist[v]) {
565+
dist[v] = wNext;
566+
pq.enqueue(v, dist[v]);
567+
}
568+
}
569+
}
570+
571+
return dist.filter(d => d <= distanceThreshold).length;
572+
};
573+
574+
let ans = n;
575+
let cnt = MAX;
576+
for (let i = n - 1; i >= 0; --i) {
577+
const t = dijkstra(i);
578+
if (t < cnt) {
579+
cnt = t;
580+
ans = i;
581+
}
582+
}
583+
584+
return ans;
585+
}
586+
```
587+
588+
<!-- tabs:end -->
589+
590+
<!-- solution:end -->
591+
529592
<!-- problem:end -->

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,67 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
508508

509509
<!-- solution:end -->
510510

511+
<!-- solution:start -->
512+
513+
### Solution 3
514+
515+
<!-- tabs:start -->
516+
517+
#### TypeScript
518+
519+
```ts
520+
function findTheCity(n: number, edges: number[][], distanceThreshold: number): number {
521+
const MAX = Number.POSITIVE_INFINITY;
522+
const g = Array.from({ length: n }, () => new Map<number, number>());
523+
const dist: number[] = Array(n).fill(MAX);
524+
const vis: boolean[] = Array(n).fill(false);
525+
for (const [f, t, w] of edges) {
526+
g[f].set(t, w);
527+
g[t].set(f, w);
528+
}
529+
530+
const dijkstra = (u: number): number => {
531+
dist.fill(MAX);
532+
vis.fill(false);
533+
dist[u] = 0;
534+
const pq = new MinPriorityQueue();
535+
pq.enqueue(u, 0);
536+
537+
while (!pq.isEmpty()) {
538+
const u = pq.dequeue().element;
539+
if (vis[u]) continue;
540+
vis[u] = true;
541+
542+
for (const [v, w] of g[u]) {
543+
if (vis[v]) continue;
544+
545+
const wNext = dist[u] + w;
546+
if (wNext < dist[v]) {
547+
dist[v] = wNext;
548+
pq.enqueue(v, dist[v]);
549+
}
550+
}
551+
}
552+
553+
return dist.filter(d => d <= distanceThreshold).length;
554+
};
555+
556+
let ans = n;
557+
let cnt = MAX;
558+
for (let i = n - 1; i >= 0; --i) {
559+
const t = dijkstra(i);
560+
if (t < cnt) {
561+
cnt = t;
562+
ans = i;
563+
}
564+
}
565+
566+
return ans;
567+
}
568+
```
569+
570+
<!-- tabs:end -->
571+
572+
<!-- solution:end -->
573+
511574
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function findTheCity(n: number, edges: number[][], distanceThreshold: number): number {
2+
const MAX = Number.POSITIVE_INFINITY;
3+
const g = Array.from({ length: n }, () => new Map<number, number>());
4+
const dist: number[] = Array(n).fill(MAX);
5+
const vis: boolean[] = 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: number): number => {
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)