Skip to content

Commit 54958ec

Browse files
solves #684: Redundant Connection in java
1 parent bfb5f6c commit 54958ec

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@
379379
| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence) | [![Java](assets/java.png)](src/LongestContinuousIncreasingSubsequence.java) [![Python](assets/python.png)](python/longest_continuous_increasing_subsequence.py) | |
380380
| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii) | [![Java](assets/java.png)](src/ValidPalindromeII.java) [![Python](assets/python.png)](python/valid_pallindrome_ii.py) | |
381381
| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game) | [![Java](assets/java.png)](src/BaseballGame.java) [![Python](assets/python.png)](python/baseball_game.py) | |
382+
| 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection) | [![Java](assets/java.png)](src/RedundantConnection.java) | |
382383
| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match) | | |
383384
| 687 | [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path) | | |
384385
| 688 | [Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard) | [![Java](assets/java.png)](src/KnightProbabilityInChessboard.java) | |

src/GraphValidTree.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public void union(int x, int y) {
3131
if (rootX == rootY) {
3232
return;
3333
}
34-
if (root[rootX] < root[rootY]) {
34+
if (rank[rootX] < rank[rootY]) {
3535
root[rootX] = rootY;
36-
} else if (root[rootX] > root[rootY]) {
36+
} else if (rank[rootX] > rank[rootY]) {
3737
root[rootY] = rootX;
3838
} else {
3939
root[rootY] = rootX;

src/RedundantConnection.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// https://leetcode.com/problems/redundant-connection
2+
// T: O(N)
3+
// S: O(N)
4+
5+
public class RedundantConnection {
6+
private static final class DisjointSet {
7+
private final int[] root, rank;
8+
9+
public DisjointSet(int size) {
10+
this.root = new int[size];
11+
this.rank = new int[size];
12+
for (int i = 0 ; i < size ; i++) {
13+
root[i] = i;
14+
rank[i] = 1;
15+
}
16+
}
17+
18+
public int find(int num) {
19+
if (num == root[num]) {
20+
return num;
21+
}
22+
return root[num] = find(root[num]);
23+
}
24+
25+
public boolean areConnected(int x, int y) {
26+
return find(x) == find(y);
27+
}
28+
29+
public void union(int x, int y) {
30+
final int rootX = find(x), rootY = find(y);
31+
if (rootX == rootY) {
32+
return;
33+
}
34+
if (rank[rootX] < rank[rootY]) {
35+
root[rootY] = rootX;
36+
} else if (rank[rootX] > rank[rootY]) {
37+
root[rootY] = rootX;
38+
} else {
39+
root[rootY] = rootX;
40+
rank[rootX]++;
41+
}
42+
}
43+
}
44+
45+
public int[] findRedundantConnection(int[][] edges) {
46+
final DisjointSet disjointSet = new DisjointSet(edges.length);
47+
for (int[] edge : edges) {
48+
if (disjointSet.areConnected(edge[0] - 1, edge[1] - 1)) {
49+
return edge;
50+
}
51+
disjointSet.union(edge[0] - 1, edge[1] - 1);
52+
}
53+
return new int[] {};
54+
}
55+
}

0 commit comments

Comments
 (0)