From e453452f0462f0b776ea7f7cf34ac3cfe531e17a Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 1 Jun 2024 15:35:22 +0300 Subject: [PATCH 1/4] refactor: update ts solution to lc problem: No.323 --- .../README.md | 39 +++++++++---------- .../README_EN.md | 39 +++++++++---------- .../Solution3.ts | 39 +++++++++---------- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md index 717678852aa5f..c494d31828fc5 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md @@ -643,30 +643,29 @@ func countComponents(n int, edges [][]int) (ans int) { ```ts function countComponents(n: number, edges: number[][]): number { - const g: number[][] = Array.from({ length: n }, () => []); + const graph: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); + const vis = new Set(); + let ans = 0; + for (const [a, b] of edges) { - g[a].push(b); - g[b].push(a); + graph.get(a)!.push(b); + graph.get(b)!.push(a); } - const vis: boolean[] = Array(n).fill(false); - let ans = 0; - for (let i = 0; i < n; ++i) { - if (vis[i]) { - continue; - } - vis[i] = true; - ++ans; - const q: number[] = [i]; - while (q.length) { - const a = q.pop()!; - for (const b of g[a]) { - if (!vis[b]) { - vis[b] = true; - q.push(b); - } - } + + for (const [i] of graph) { + if (vis.has(i)) continue; + + const q = [i]; + for (const j of q) { + if (vis.has(j)) continue; + + vis.add(j); + q.push(...graph.get(j)!); } + + ans++; } + return ans; } ``` diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md index cd2ee66d24707..678018d9ed033 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md @@ -638,30 +638,29 @@ func countComponents(n int, edges [][]int) (ans int) { ```ts function countComponents(n: number, edges: number[][]): number { - const g: number[][] = Array.from({ length: n }, () => []); + const graph: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); + const vis = new Set(); + let ans = 0; + for (const [a, b] of edges) { - g[a].push(b); - g[b].push(a); + graph.get(a)!.push(b); + graph.get(b)!.push(a); } - const vis: boolean[] = Array(n).fill(false); - let ans = 0; - for (let i = 0; i < n; ++i) { - if (vis[i]) { - continue; - } - vis[i] = true; - ++ans; - const q: number[] = [i]; - while (q.length) { - const a = q.pop()!; - for (const b of g[a]) { - if (!vis[b]) { - vis[b] = true; - q.push(b); - } - } + + for (const [i] of graph) { + if (vis.has(i)) continue; + + const q = [i]; + for (const j of q) { + if (vis.has(j)) continue; + + vis.add(j); + q.push(...graph.get(j)!); } + + ans++; } + return ans; } ``` diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts index 14ff21daffa41..1c31e68891f7e 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts @@ -1,27 +1,26 @@ function countComponents(n: number, edges: number[][]): number { - const g: number[][] = Array.from({ length: n }, () => []); + const graph: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); + const vis = new Set(); + let ans = 0; + for (const [a, b] of edges) { - g[a].push(b); - g[b].push(a); + graph.get(a)!.push(b); + graph.get(b)!.push(a); } - const vis: boolean[] = Array(n).fill(false); - let ans = 0; - for (let i = 0; i < n; ++i) { - if (vis[i]) { - continue; - } - vis[i] = true; - ++ans; - const q: number[] = [i]; - while (q.length) { - const a = q.pop()!; - for (const b of g[a]) { - if (!vis[b]) { - vis[b] = true; - q.push(b); - } - } + + for (const [i] of graph) { + if (vis.has(i)) continue; + + const q = [i]; + for (const j of q) { + if (vis.has(j)) continue; + + vis.add(j); + q.push(...graph.get(j)!); } + + ans++; } + return ans; } From 64b6694d2a27112730f1811f6ff64cffb19286fa Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 1 Jun 2024 22:10:28 +0800 Subject: [PATCH 2/4] Update README.md --- .../README.md | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md index c494d31828fc5..14c7530a130d0 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md @@ -643,29 +643,28 @@ func countComponents(n int, edges [][]int) (ans int) { ```ts function countComponents(n: number, edges: number[][]): number { - const graph: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); - const vis = new Set(); - let ans = 0; - + const g: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); for (const [a, b] of edges) { - graph.get(a)!.push(b); - graph.get(b)!.push(a); + g.get(a)!.push(b); + g.get(b)!.push(a); } - for (const [i] of graph) { - if (vis.has(i)) continue; - + const vis = new Set(); + let ans = 0; + for (const [i] of g) { + if (vis.has(i)) { + continue; + } const q = [i]; for (const j of q) { - if (vis.has(j)) continue; - + if (vis.has(j)) { + continue; + } vis.add(j); - q.push(...graph.get(j)!); + q.push(...g.get(j)!); } - ans++; } - return ans; } ``` From b5ce339b3b362d602f39ad15a80eb8f4a411443c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 1 Jun 2024 22:11:00 +0800 Subject: [PATCH 3/4] Update README_EN.md --- .../README_EN.md | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md index 678018d9ed033..ca0cd4ff31fc3 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md @@ -638,29 +638,28 @@ func countComponents(n int, edges [][]int) (ans int) { ```ts function countComponents(n: number, edges: number[][]): number { - const graph: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); - const vis = new Set(); - let ans = 0; - + const g: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); for (const [a, b] of edges) { - graph.get(a)!.push(b); - graph.get(b)!.push(a); + g.get(a)!.push(b); + g.get(b)!.push(a); } - for (const [i] of graph) { - if (vis.has(i)) continue; - + const vis = new Set(); + let ans = 0; + for (const [i] of g) { + if (vis.has(i)) { + continue; + } const q = [i]; for (const j of q) { - if (vis.has(j)) continue; - + if (vis.has(j)) { + continue; + } vis.add(j); - q.push(...graph.get(j)!); + q.push(...g.get(j)!); } - ans++; } - return ans; } ``` From 896c2f16ad33b983f94497a428b0470a7f7b2eab Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 1 Jun 2024 22:11:23 +0800 Subject: [PATCH 4/4] Update Solution3.ts --- .../Solution3.ts | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts index 1c31e68891f7e..2823446fbdaab 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution3.ts @@ -1,26 +1,25 @@ function countComponents(n: number, edges: number[][]): number { - const graph: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); - const vis = new Set(); - let ans = 0; - + const g: Map = new Map(Array.from({ length: n }, (_, i) => [i, []])); for (const [a, b] of edges) { - graph.get(a)!.push(b); - graph.get(b)!.push(a); + g.get(a)!.push(b); + g.get(b)!.push(a); } - for (const [i] of graph) { - if (vis.has(i)) continue; - + const vis = new Set(); + let ans = 0; + for (const [i] of g) { + if (vis.has(i)) { + continue; + } const q = [i]; for (const j of q) { - if (vis.has(j)) continue; - + if (vis.has(j)) { + continue; + } vis.add(j); - q.push(...graph.get(j)!); + q.push(...g.get(j)!); } - ans++; } - return ans; }