-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_684.java
133 lines (119 loc) · 4.36 KB
/
_684.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
125
126
127
128
129
130
131
132
133
package com.fishercoder.solutions.firstthousand;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class _684 {
public static class Solution1 {
/*
* This is my original solution. A little verbose.
*/
class UnionFind {
int[] ids;
Set<Integer> nodes;
Set<Integer> visitedNodes;
int[] redundantConn;
int m;
int n;
public UnionFind(int[][] edges) {
m = edges.length;
n = edges[0].length;
nodes = new HashSet<>();
visitedNodes = new HashSet<>();
redundantConn = new int[] {};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
nodes.add(edges[i][j]);
}
}
ids = new int[nodes.size()];
for (int i = 0; i < ids.length; i++) {
ids[i] = i + 1;
}
}
public int[] union(int[] edge) {
int x = find(edge[0] - 1);
int y = find(edge[1] - 1);
if (x == y && visitedNodes.contains(edge[0]) && visitedNodes.contains(edge[1])) {
redundantConn = edge;
}
if (x != y) {
if (x < y) {
ids[y] = x + 1;
} else {
ids[x] = y + 1;
}
}
visitedNodes.add(edge[0]);
visitedNodes.add(edge[1]);
return redundantConn;
}
private int find(int id) {
if (isTree()) {
return ids[id];
}
if ((id + 1) != ids[id]) {
return find(ids[id] - 1);
}
return id;
}
private boolean isTree() {
Set<Integer> set = new HashSet<>();
for (int i : ids) {
set.add(i);
}
return set.size() == 1;
}
}
public int[] findRedundantConnection(int[][] edges) {
UnionFind unionFind = new UnionFind(edges);
int[] result = new int[] {};
for (int[] edge : edges) {
result = unionFind.union(edge);
}
return result;
}
}
public static class Solution2 {
/*
* DFS, credit: https://leetcode.com/problems/redundant-connection/editorial/
* 1. we build the graph one edge at a time, each time, we add both edge[0] to the neighbors of edge[1] and vice versa since this is an un-directed graph;
* 2. as soon as we encounter an edge that can connect to each other, it must be the redundant one.
* In other words, we first check if this new edge is needed or not based on the current existing graph:
* if the two nodes connected by this edge is already connected, then this edge is redundant.
*/
private static final int MAX_VERTICES = 1000;
public int[] findRedundantConnection(int[][] edges) {
List<Integer>[] graph = new ArrayList[MAX_VERTICES + 1];
for (int i = 0; i < graph.length; i++) {
graph[i] = new ArrayList<>();
}
Set<Integer> visited = new HashSet<>();
for (int[] edge : edges) {
visited.clear();
if (!graph[edge[0]].isEmpty()
&& !graph[edge[1]].isEmpty()
&& canConnect(edge[0], edge[1], graph, visited)) {
return edge;
}
graph[edge[0]].add(edge[1]);
graph[edge[1]].add(edge[0]);
}
return null;
}
private boolean canConnect(
int source, int target, List<Integer>[] graph, Set<Integer> visited) {
if (visited.add(source)) {
if (source == target) {
return true;
}
for (int v : graph[target]) {
if (canConnect(source, v, graph, visited)) {
return true;
}
}
}
return false;
}
}
}