File tree 3 files changed +58
-2
lines changed
3 files changed +58
-2
lines changed Original file line number Diff line number Diff line change 379
379
| 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 ) | |
380
380
| 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 ) | |
381
381
| 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 ) | |
382
383
| 686 | [ Repeated String Match] ( https://leetcode.com/problems/repeated-string-match ) | | |
383
384
| 687 | [ Longest Univalue Path] ( https://leetcode.com/problems/longest-univalue-path ) | | |
384
385
| 688 | [ Knight Probability in Chessboard] ( https://leetcode.com/problems/knight-probability-in-chessboard ) | [ ![ Java] ( assets/java.png )] ( src/KnightProbabilityInChessboard.java ) | |
Original file line number Diff line number Diff line change @@ -31,9 +31,9 @@ public void union(int x, int y) {
31
31
if (rootX == rootY ) {
32
32
return ;
33
33
}
34
- if (root [rootX ] < root [rootY ]) {
34
+ if (rank [rootX ] < rank [rootY ]) {
35
35
root [rootX ] = rootY ;
36
- } else if (root [rootX ] > root [rootY ]) {
36
+ } else if (rank [rootX ] > rank [rootY ]) {
37
37
root [rootY ] = rootX ;
38
38
} else {
39
39
root [rootY ] = rootX ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments