Skip to content

Commit

Permalink
add 323 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 12, 2024
1 parent 46bfab1 commit a55ea2b
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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']
Expand All @@ -22,27 +22,27 @@
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']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
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)']
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> 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<List<Integer>> 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<List<Integer>> 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<Integer> 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;
}

}

0 comments on commit a55ea2b

Please sign in to comment.