-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberOfOperationsToMakeNetworkConnected.java
More file actions
54 lines (43 loc) · 1.46 KB
/
NumberOfOperationsToMakeNetworkConnected.java
File metadata and controls
54 lines (43 loc) · 1.46 KB
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
package graph;
/**
* Description: https://leetcode.com/problems/number-of-operations-to-make-network-connected
* Difficulty: Medium
* Time complexity: O(n)
* Space complexity: O(n)
*/
public class NumberOfOperationsToMakeNetworkConnected {
public int makeConnectedViaUnionFind(int n, int[][] connections) {
if (connections.length < n - 1) return -1; // not enough cables
int[] parents = new int[n];
int[] ranks = new int[n];
for (int i = 0; i < n; i++) {
parents[i] = i;
ranks[i] = 1;
}
int components = n;
for (int[] connection : connections) {
components -= uniteAndCount(parents, ranks, connection[0], connection[1]);
}
return components - 1;
}
private int uniteAndCount(int[] parents, int[] ranks, int node1, int node2) {
int parent1 = findParent(parents, node1);
int parent2 = findParent(parents, node2);
if (parent1 == parent2) return 0;
if (ranks[parent1] > ranks[parent2]) {
parents[parent2] = parent1;
ranks[parent1] += ranks[parent2];
} else {
parents[parent1] = parent2;
ranks[parent2] += ranks[parent1];
}
return 1;
}
private int findParent(int[] parents, int node) {
while (node != parents[node]) {
parents[node] = parents[parents[node]];
node = parents[node];
}
return node;
}
}