Skip to content

Commit dfd7c06

Browse files
committed
feat: add js solution to lc problem: No. 1334
1 parent b9a6042 commit dfd7c06

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,36 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
566566
}
567567
```
568568

569+
#### JavaScript
570+
571+
```js
572+
function findTheCity(n, edges, distanceThreshold) {
573+
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
574+
for (const [f, t, w] of edges) {
575+
g[f][t] = g[t][f] = w;
576+
}
577+
for (let k = 0; k < n; ++k) {
578+
g[k][k] = 0;
579+
for (let i = 0; i < n; ++i) {
580+
for (let j = 0; j < n; ++j) {
581+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
582+
}
583+
}
584+
}
585+
586+
let ans = n,
587+
cnt = n + 1;
588+
for (let i = n - 1; i >= 0; --i) {
589+
const t = g[i].filter(x => x <= distanceThreshold).length;
590+
if (t < cnt) {
591+
cnt = t;
592+
ans = i;
593+
}
594+
}
595+
return ans;
596+
}
597+
```
598+
569599
<!-- tabs:end -->
570600

571601
<!-- solution:end -->

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,36 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
548548
}
549549
```
550550

551+
#### JavaScript
552+
553+
```js
554+
function findTheCity(n, edges, distanceThreshold) {
555+
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
556+
for (const [f, t, w] of edges) {
557+
g[f][t] = g[t][f] = w;
558+
}
559+
for (let k = 0; k < n; ++k) {
560+
g[k][k] = 0;
561+
for (let i = 0; i < n; ++i) {
562+
for (let j = 0; j < n; ++j) {
563+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
564+
}
565+
}
566+
}
567+
568+
let ans = n,
569+
cnt = n + 1;
570+
for (let i = n - 1; i >= 0; --i) {
571+
const t = g[i].filter(x => x <= distanceThreshold).length;
572+
if (t < cnt) {
573+
cnt = t;
574+
ans = i;
575+
}
576+
}
577+
return ans;
578+
}
579+
```
580+
551581
<!-- tabs:end -->
552582

553583
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function findTheCity(n, edges, distanceThreshold) {
2+
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
3+
for (const [f, t, w] of edges) {
4+
g[f][t] = g[t][f] = w;
5+
}
6+
for (let k = 0; k < n; ++k) {
7+
g[k][k] = 0;
8+
for (let i = 0; i < n; ++i) {
9+
for (let j = 0; j < n; ++j) {
10+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
11+
}
12+
}
13+
}
14+
15+
let ans = n,
16+
cnt = n + 1;
17+
for (let i = n - 1; i >= 0; --i) {
18+
const t = g[i].filter(x => x <= distanceThreshold).length;
19+
if (t < cnt) {
20+
cnt = t;
21+
ans = i;
22+
}
23+
}
24+
return ans;
25+
}

0 commit comments

Comments
 (0)