Skip to content

Commit b9a6042

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

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,50 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
329329
}
330330
```
331331

332+
#### JavaScript
333+
334+
```js
335+
function findTheCity(n, edges, distanceThreshold) {
336+
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
337+
const dist = Array(n).fill(Infinity);
338+
const vis = Array(n).fill(false);
339+
for (const [f, t, w] of edges) {
340+
g[f][t] = g[t][f] = w;
341+
}
342+
343+
const dijkstra = u => {
344+
dist.fill(Infinity);
345+
vis.fill(false);
346+
dist[u] = 0;
347+
for (let i = 0; i < n; ++i) {
348+
let k = -1;
349+
for (let j = 0; j < n; ++j) {
350+
if (!vis[j] && (k === -1 || dist[j] < dist[k])) {
351+
k = j;
352+
}
353+
}
354+
vis[k] = true;
355+
for (let j = 0; j < n; ++j) {
356+
dist[j] = Math.min(dist[j], dist[k] + g[k][j]);
357+
}
358+
}
359+
return dist.filter(d => d <= distanceThreshold).length;
360+
};
361+
362+
let ans = n;
363+
let cnt = Infinity;
364+
for (let i = n - 1; i >= 0; --i) {
365+
const t = dijkstra(i);
366+
if (t < cnt) {
367+
cnt = t;
368+
ans = i;
369+
}
370+
}
371+
372+
return ans;
373+
}
374+
```
375+
332376
<!-- tabs:end -->
333377

334378
<!-- solution:end -->

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,50 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
319319
}
320320
```
321321

322+
#### JavaScript
323+
324+
```js
325+
function findTheCity(n, edges, distanceThreshold) {
326+
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
327+
const dist = Array(n).fill(Infinity);
328+
const vis = Array(n).fill(false);
329+
for (const [f, t, w] of edges) {
330+
g[f][t] = g[t][f] = w;
331+
}
332+
333+
const dijkstra = u => {
334+
dist.fill(Infinity);
335+
vis.fill(false);
336+
dist[u] = 0;
337+
for (let i = 0; i < n; ++i) {
338+
let k = -1;
339+
for (let j = 0; j < n; ++j) {
340+
if (!vis[j] && (k === -1 || dist[j] < dist[k])) {
341+
k = j;
342+
}
343+
}
344+
vis[k] = true;
345+
for (let j = 0; j < n; ++j) {
346+
dist[j] = Math.min(dist[j], dist[k] + g[k][j]);
347+
}
348+
}
349+
return dist.filter(d => d <= distanceThreshold).length;
350+
};
351+
352+
let ans = n;
353+
let cnt = Infinity;
354+
for (let i = n - 1; i >= 0; --i) {
355+
const t = dijkstra(i);
356+
if (t < cnt) {
357+
cnt = t;
358+
ans = i;
359+
}
360+
}
361+
362+
return ans;
363+
}
364+
```
365+
322366
<!-- tabs:end -->
323367

324368
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function findTheCity(n, edges, distanceThreshold) {
2+
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
3+
const dist = Array(n).fill(Infinity);
4+
const vis = Array(n).fill(false);
5+
for (const [f, t, w] of edges) {
6+
g[f][t] = g[t][f] = w;
7+
}
8+
9+
const dijkstra = u => {
10+
dist.fill(Infinity);
11+
vis.fill(false);
12+
dist[u] = 0;
13+
for (let i = 0; i < n; ++i) {
14+
let k = -1;
15+
for (let j = 0; j < n; ++j) {
16+
if (!vis[j] && (k === -1 || dist[j] < dist[k])) {
17+
k = j;
18+
}
19+
}
20+
vis[k] = true;
21+
for (let j = 0; j < n; ++j) {
22+
dist[j] = Math.min(dist[j], dist[k] + g[k][j]);
23+
}
24+
}
25+
return dist.filter(d => d <= distanceThreshold).length;
26+
};
27+
28+
let ans = n;
29+
let cnt = Infinity;
30+
for (let i = n - 1; i >= 0; --i) {
31+
const t = dijkstra(i);
32+
if (t < cnt) {
33+
cnt = t;
34+
ans = i;
35+
}
36+
}
37+
38+
return ans;
39+
}

0 commit comments

Comments
 (0)