-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathRedundantConnection.java
124 lines (103 loc) · 3.3 KB
/
RedundantConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package LeetCodeJava.Tree;
// https://leetcode.com/problems/redundant-connection/description/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class RedundantConnection {
// V0
// TODO : implement
// V1
// https://www.youtube.com/watch?v=FXWRE67PLL0
// https://github.com/neetcode-gh/leetcode/blob/main/java/0684-redundant-connection.java
int[] parent;
public int[] findRedundantConnection_1(int[][] edges) {
parent = new int[edges.length];
for (int i = 0; i < edges.length; i++) {
parent[i] = i + 1;
}
for (int[] edge : edges) {
if (find(edge[0]) == find(edge[1])) {
return edge;
} else {
union(edge[0], edge[1]);
}
}
return new int[2];
}
public int find(int x) {
if (x == parent[x - 1]) return x;
return find(parent[x - 1]);
}
public void union(int x, int y) {
parent[find(y) - 1] = find(x);
}
// V2
// IDEA : DFS
// https://leetcode.com/problems/redundant-connection/editorial/
Set<Integer> seen = new HashSet();
int MAX_EDGE_VAL = 1000;
public int[] findRedundantConnection_2(int[][] edges) {
ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];
for (int i = 0; i <= MAX_EDGE_VAL; i++) {
graph[i] = new ArrayList();
}
for (int[] edge : edges) {
// NOTE !!! clear seen before call dfs
seen.clear();
if (!graph[edge[0]].isEmpty() && !graph[edge[1]].isEmpty() && dfs(graph, edge[0], edge[1])) {
return edge;
}
graph[edge[0]].add(edge[1]);
graph[edge[1]].add(edge[0]);
}
throw new AssertionError();
}
public boolean dfs(ArrayList<Integer>[] graph, int source, int target) {
if (!seen.contains(source)) {
seen.add(source);
if (source == target) return true;
for (int nei : graph[source]) {
if (dfs(graph, nei, target)) return true;
}
}
return false;
}
// V3
// IDEA : UNION FIND
// https://leetcode.com/problems/redundant-connection/editorial/
int MAX_EDGE_VAL_1 = 1000;
public int[] findRedundantConnection_3(int[][] edges) {
DSU dsu = new DSU(MAX_EDGE_VAL_1 + 1);
for (int[] edge : edges) {
if (!dsu.union(edge[0], edge[1])) return edge;
}
throw new AssertionError();
}
class DSU {
int[] parent;
int[] rank;
public DSU(int size) {
parent = new int[size];
for (int i = 0; i < size; i++) parent[i] = i;
rank = new int[size];
}
public int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
public boolean union(int x, int y) {
int xr = find(x), yr = find(y);
if (xr == yr) {
return false;
} else if (rank[xr] < rank[yr]) {
parent[xr] = yr;
} else if (rank[xr] > rank[yr]) {
parent[yr] = xr;
} else {
parent[yr] = xr;
rank[xr]++;
}
return true;
}
}
}