From a55ea2b32a3488a2d33109d82cf115d054eb7d98 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Tue, 12 Mar 2024 23:44:55 +0800 Subject: [PATCH] add 323 java, update progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +-- ...rOfConnectedComponentsUndirectedGraph.java | 143 ++++++++++++++++++ 4 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Graph/NumberOfConnectedComponentsUndirectedGraph.java diff --git a/README.md b/README.md index 819028d7..8e3b3aa7 100644 --- a/README.md +++ b/README.md @@ -1127,7 +1127,7 @@ | # | Title | Solution | Time | Space | Difficulty | Status | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 269 |[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/) | [Python](./leetcode_python/Graph/alien-dictionary.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Graph/AlienDictionary.java) | | | Hard| Curated Top 75, dfs, bfs, topology sort, `fb`, google, m$, airbnb, uber, amazon | AGAIN******** (4) (not start) -323| [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [Python ](./leetcode_python/Graph/number-of-connected-components-in-an-undirected-graph.py) | _O(n)_ | _O(n)_| Medium | Curated Top 75, 🔒 , dfs, Union Find, `graph`, `linkedin`, `amazon`, fb, google, m$, linkedin| AGAIN************** (5) (MUST) +323| [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [Python ](./leetcode_python/Graph/number-of-connected-components-in-an-undirected-graph.py), [Java ](./leetcode_java/src/main/java/LeetCodeJava/Graph/NumberOfConnectedComponentsUndirectedGraph.java) | _O(n)_ | _O(n)_| Medium | Curated Top 75, 🔒 , dfs, Union Find, `graph`, `linkedin`, `amazon`, fb, google, m$, linkedin| AGAIN************** (5) (MUST) 959 | [Regions Cut By Slashes](https://leetcode.com/problems/regions-cut-by-slashes/) | [Python](./leetcode_python/Graph/regions-cut-by-slashes.py) | _O(n^2)_| _O(n^2)_| Medium | Union Find | 1135 | [Connecting Cities With Minimum Cost](https://leetcode.com/problems/connecting-cities-with-minimum-cost/) | [Python](./leetcode_python/Graph/connecting-cities-with-minimum-cost.py) | _O(n^2)_| _O(n^2)_| Medium | union find, Kruskal, prime, graph, amazon | AGAIN (not start) diff --git a/data/progress.txt b/data/progress.txt index 798553ff..c447edbd 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240312: 297(again),300,322(again) +20240312: 297(again),300,322(again),323 20240311: 261(again!!!),268,269(again),271,295(again) 20240310: 213,217,226,230,235,238,242,252,253 20240309: 207(again!!!),208,211(again),212 diff --git a/data/to_review.txt b/data/to_review.txt index 0c171778..fe920ec7 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-05-06 -> ['297(again),300,322(again)'] +2024-05-06 -> ['297(again),300,322(again),323'] 2024-05-05 -> ['261(again!!!),268,269(again),271,295(again)'] 2024-05-04 -> ['213,217,226,230,235,238,242,252,253'] 2024-05-03 -> ['207(again!!!),208,211(again),212'] @@ -11,7 +11,7 @@ 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] -2024-04-15 -> ['297(again),300,322(again)'] +2024-04-15 -> ['297(again),300,322(again),323'] 2024-04-14 -> ['261(again!!!),268,269(again),271,295(again)'] 2024-04-13 -> ['213,217,226,230,235,238,242,252,253'] 2024-04-12 -> ['207(again!!!),208,211(again),212'] @@ -22,7 +22,7 @@ 2024-04-07 -> ['73,76,79(again),91,25'] 2024-04-06 -> ['55(again),56,62,70'] 2024-04-03 -> ['39,48(again),49,53,54'] -2024-04-02 -> ['297(again),300,322(again)', '20,21,23,33(again)'] +2024-04-02 -> ['297(again),300,322(again),323', '20,21,23,33(again)'] 2024-04-01 -> ['261(again!!!),268,269(again),271,295(again)', '1,3,5,4,19'] 2024-03-31 -> ['213,217,226,230,235,238,242,252,253'] 2024-03-30 -> ['207(again!!!),208,211(again),212'] @@ -30,19 +30,19 @@ 2024-03-28 -> ['139(again),141,143(again)'] 2024-03-27 -> ['121,124(again),125,128,133(again)'] 2024-03-26 -> ['100,102,104,105(again)'] -2024-03-25 -> ['297(again),300,322(again)', '73,76,79(again),91,25'] +2024-03-25 -> ['297(again),300,322(again),323', '73,76,79(again),91,25'] 2024-03-24 -> ['261(again!!!),268,269(again),271,295(again)', '55(again),56,62,70'] 2024-03-23 -> ['213,217,226,230,235,238,242,252,253'] 2024-03-22 -> ['207(again!!!),208,211(again),212'] 2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54'] -2024-03-20 -> ['297(again),300,322(again)', '139(again),141,143(again)', '20,21,23,33(again)'] +2024-03-20 -> ['297(again),300,322(again),323', '139(again),141,143(again)', '20,21,23,33(again)'] 2024-03-19 -> ['261(again!!!),268,269(again),271,295(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19'] 2024-03-18 -> ['213,217,226,230,235,238,242,252,253', '100,102,104,105(again)'] -2024-03-17 -> ['297(again),300,322(again)', '207(again!!!),208,211(again),212', '73,76,79(again),91,25'] +2024-03-17 -> ['297(again),300,322(again),323', '207(again!!!),208,211(again),212', '73,76,79(again),91,25'] 2024-03-16 -> ['261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70'] -2024-03-15 -> ['297(again),300,322(again)', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)'] -2024-03-14 -> ['297(again),300,322(again)', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)'] -2024-03-13 -> ['297(again),300,322(again)', '261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54'] +2024-03-15 -> ['297(again),300,322(again),323', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)'] +2024-03-14 -> ['297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)'] +2024-03-13 -> ['297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54'] 2024-03-12 -> ['261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-11 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-10 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Graph/NumberOfConnectedComponentsUndirectedGraph.java b/leetcode_java/src/main/java/LeetCodeJava/Graph/NumberOfConnectedComponentsUndirectedGraph.java new file mode 100644 index 00000000..593eacc3 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Graph/NumberOfConnectedComponentsUndirectedGraph.java @@ -0,0 +1,143 @@ +package LeetCodeJava.Graph; + +// https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/description/?envType=list&envId=xoqag3yj +// https://leetcode.ca/all/323.html +// https://leetcode.ca/2016-10-18-323-Number-of-Connected-Components-in-an-Undirected-Graph/ + +import java.util.*; + +public class NumberOfConnectedComponentsUndirectedGraph { + + // V0 + // IDEA : GRAPH + // TODO : implement it +// public int countComponents(int n, int[][] edges) { +// return 0; +// } + + // V1 + // IDEA : UNION FIND + // https://leetcode.ca/2016-10-18-323-Number-of-Connected-Components-in-an-Undirected-Graph/ + private int[] p; + + public int countComponents_1(int n, int[][] edges) { + p = new int[n]; + for (int i = 0; i < n; ++i) { + p[i] = i; + } + for (int[] e : edges) { + int a = e[0], b = e[1]; + p[find(a)] = find(b); + } + int ans = 0; + for (int i = 0; i < n; ++i) { + if (i == find(i)) { + ++ans; + } + } + return ans; + } + + private int find(int x) { + if (p[x] != x) { + p[x] = find(p[x]); + } + return p[x]; + } + + // V2 + // IDEA : DFS + // https://www.cnblogs.com/cnoodle/p/14197652.html + public int countComponents_2(int n, int[][] edges) { + int count = 0; + List> g = new ArrayList<>(); + boolean[] visited = new boolean[n]; + for (int i = 0; i < n; i++) { + g.add(new ArrayList<>()); + } + for (int[] e : edges) { + g.get(e[0]).add(e[1]); + g.get(e[1]).add(e[0]); + } + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + count++; + dfs(visited, i, g); + } + } + return count; + } + + private void dfs(boolean[] visited, int node, List> g) { + visited[node] = true; + for (int next : g.get(node)) { + if (!visited[next]) { + dfs(visited, next, g); + } + } + } + + // V3 + // IDEA : UNION FIND + // https://www.cnblogs.com/cnoodle/p/14197652.html + public int countComponents_3(int n, int[][] edges) { + int count = n; + int[] parents = new int[n]; + for (int i = 0; i < n; i++) { + parents[i] = i; + } + for (int[] edge : edges) { + int p = find(parents, edge[0]); + int q = find(parents, edge[1]); + if (p != q) { + parents[p] = q; + count--; + } + } + return count; + } + + private int find(int[] parents, int i) { + while (parents[i] != i) { + parents[i] = parents[parents[i]]; // route compression + i = parents[i]; + } + return i; + } + + // V4 + // IDEA : BFS + // https://www.cnblogs.com/cnoodle/p/14197652.html + public int countComponents_4(int n, int[][] edges) { + int count = 0; + List> g = new ArrayList<>(); + boolean[] visited = new boolean[n]; + for (int i = 0; i < n; i++) { + g.add(new ArrayList<>()); + } + for (int[] e : edges) { + g.get(e[0]).add(e[1]); + g.get(e[1]).add(e[0]); + } + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + count++; + Queue queue = new LinkedList<>(); + queue.offer(i); + while (!queue.isEmpty()) { + int index = queue.poll(); + visited[index] = true; + for (int next : g.get(index)) { + if (!visited[next]) { + queue.offer(next); + } + } + } + } + } + return count; + } + +}