From 98a4c740df734f8bfa02244db538334860e3514a Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 29 Jul 2024 17:14:18 +0300 Subject: [PATCH 1/4] feat: add ts solution to lc problem: No.1334 --- .../README.md | 63 +++++++++++++++++++ .../README_EN.md | 63 +++++++++++++++++++ .../Solution3.ts | 48 ++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.ts diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md index 16fea24c5faae..78ae31fbb3b6e 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md @@ -526,4 +526,67 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n + + +### Solution 3 + + + +#### TypeScript + +```ts +function findTheCity(n: number, edges: number[][], distanceThreshold: number): number { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + 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; +} +``` + + + + + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md index 20d960a5eaa5f..a99512e4f5535 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md @@ -508,4 +508,67 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n + + +### Solution 3 + + + +#### TypeScript + +```ts +function findTheCity(n: number, edges: number[][], distanceThreshold: number): number { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + 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; +} +``` + + + + + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.ts b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.ts new file mode 100644 index 0000000000000..cd8545d7a82c9 --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.ts @@ -0,0 +1,48 @@ +function findTheCity(n: number, edges: number[][], distanceThreshold: number): number { + const MAX = Number.POSITIVE_INFINITY; + const g = Array.from({ length: n }, () => new Map()); + 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; +} From 713fd3ef488b046cea783bdf0cf9918ec3090f73 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 29 Jul 2024 17:17:18 +0300 Subject: [PATCH 2/4] feat: add js solution to lc problem: No. 1334 --- .../README.md | 53 +++++++++++++++++++ .../README_EN.md | 53 +++++++++++++++++++ .../Solution3.js | 48 +++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.js diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md index 78ae31fbb3b6e..afe63f3f93212 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md @@ -585,6 +585,59 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n } ``` +#### 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; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md index a99512e4f5535..49f08dd36d052 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md @@ -567,6 +567,59 @@ function findTheCity(n: number, edges: number[][], distanceThreshold: number): n } ``` +#### 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; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.js b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.js new file mode 100644 index 0000000000000..1ed22e36f6c56 --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution3.js @@ -0,0 +1,48 @@ +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; +} From b9a6042701ee15a89ce7a7173a78109b176bddbb Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 29 Jul 2024 17:20:19 +0300 Subject: [PATCH 3/4] feat: add js solution to lc problem: No. 1334 --- .../README.md | 44 +++++++++++++++++++ .../README_EN.md | 44 +++++++++++++++++++ .../Solution.js | 39 ++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution.js diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md index afe63f3f93212..9f07229c642f9 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md @@ -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; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md index 49f08dd36d052..f6093cc70f6bf 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md @@ -319,6 +319,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; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution.js b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution.js new file mode 100644 index 0000000000000..df5b8032bf103 --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution.js @@ -0,0 +1,39 @@ +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; +} From dfd7c060349a73a0b8c176c37a4b507a985b607c Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 29 Jul 2024 17:23:02 +0300 Subject: [PATCH 4/4] feat: add js solution to lc problem: No. 1334 --- .../README.md | 30 +++++++++++++++++++ .../README_EN.md | 30 +++++++++++++++++++ .../Solution2.js | 25 ++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution2.js diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md index 9f07229c642f9..20c633d1c9fe2 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README.md @@ -566,6 +566,36 @@ 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; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md index f6093cc70f6bf..283121786a480 100644 --- a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/README_EN.md @@ -548,6 +548,36 @@ 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; +} +``` + diff --git a/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution2.js b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution2.js new file mode 100644 index 0000000000000..eefee56d3ee4e --- /dev/null +++ b/solution/1300-1399/1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance/Solution2.js @@ -0,0 +1,25 @@ +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; +}