Skip to content

feat: add js solution to lc problem: No. 1334 #3334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,50 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
}
```

#### JavaScript

```js
function findTheCity(n, edges, distanceThreshold) {
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
const dist = Array(n).fill(Infinity);
const vis = Array(n).fill(false);
for (const [f, t, w] of edges) {
g[f][t] = g[t][f] = w;
}

const dijkstra = u => {
dist.fill(Infinity);
vis.fill(false);
dist[u] = 0;
for (let i = 0; i < n; ++i) {
let k = -1;
for (let j = 0; j < n; ++j) {
if (!vis[j] && (k === -1 || dist[j] < dist[k])) {
k = j;
}
}
vis[k] = true;
for (let j = 0; j < n; ++j) {
dist[j] = Math.min(dist[j], dist[k] + g[k][j]);
}
}
return dist.filter(d => d <= distanceThreshold).length;
};

let ans = n;
let cnt = Infinity;
for (let i = n - 1; i >= 0; --i) {
const t = dijkstra(i);
if (t < cnt) {
cnt = t;
ans = i;
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -522,6 +566,152 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n
}
```

#### JavaScript

```js
function findTheCity(n, edges, distanceThreshold) {
const g = Array.from({ length: n }, () => Array(n).fill(Infinity));
for (const [f, t, w] of edges) {
g[f][t] = g[t][f] = w;
}
for (let k = 0; k < n; ++k) {
g[k][k] = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
}
}
}

let ans = n,
cnt = n + 1;
for (let i = n - 1; i >= 0; --i) {
const t = g[i].filter(x => x <= distanceThreshold).length;
if (t < cnt) {
cnt = t;
ans = i;
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 3

<!-- tabs:start -->

#### TypeScript

```ts
function findTheCity(n: number, edges: number[][], distanceThreshold: number): number {
const MAX = Number.POSITIVE_INFINITY;
const g = Array.from({ length: n }, () => new Map<number, number>());
const dist: number[] = Array(n).fill(MAX);
const vis: boolean[] = Array(n).fill(false);
for (const [f, t, w] of edges) {
g[f].set(t, w);
g[t].set(f, w);
}

const dijkstra = (u: number): number => {
dist.fill(MAX);
vis.fill(false);
dist[u] = 0;
const pq = new MinPriorityQueue();
pq.enqueue(u, 0);

while (!pq.isEmpty()) {
const u = pq.dequeue().element;
if (vis[u]) continue;
vis[u] = true;

for (const [v, w] of g[u]) {
if (vis[v]) continue;

const wNext = dist[u] + w;
if (wNext < dist[v]) {
dist[v] = wNext;
pq.enqueue(v, dist[v]);
}
}
}

return dist.filter(d => d <= distanceThreshold).length;
};

let ans = n;
let cnt = MAX;
for (let i = n - 1; i >= 0; --i) {
const t = dijkstra(i);
if (t < cnt) {
cnt = t;
ans = i;
}
}

return ans;
}
```

#### JavaScript

```js
export function findTheCity(n, edges, distanceThreshold) {
const MAX = Number.POSITIVE_INFINITY;
const g = Array.from({ length: n }, () => new Map());
const dist = Array(n).fill(MAX);
const vis = Array(n).fill(false);
for (const [f, t, w] of edges) {
g[f].set(t, w);
g[t].set(f, w);
}

const dijkstra = u => {
dist.fill(MAX);
vis.fill(false);
dist[u] = 0;
const pq = new MinPriorityQueue();
pq.enqueue(u, 0);

while (!pq.isEmpty()) {
const u = pq.dequeue().element;
if (vis[u]) continue;
vis[u] = true;

for (const [v, w] of g[u]) {
if (vis[v]) continue;

const wNext = dist[u] + w;
if (wNext < dist[v]) {
dist[v] = wNext;
pq.enqueue(v, dist[v]);
}
}
}

return dist.filter(d => d <= distanceThreshold).length;
};

let ans = n;
let cnt = MAX;
for (let i = n - 1; i >= 0; --i) {
const t = dijkstra(i);
if (t < cnt) {
cnt = t;
ans = i;
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading