-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0952-largest-component-size-by-common-factor.js
64 lines (57 loc) · 1.68 KB
/
0952-largest-component-size-by-common-factor.js
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
/**
* 952. Largest Component Size by Common Factor
* https://leetcode.com/problems/largest-component-size-by-common-factor/
* Difficulty: Hard
*
* You are given an integer array of unique positive integers nums. Consider the following graph:
* - There are nums.length nodes, labeled nums[0] to nums[nums.length - 1],
* - There is an undirected edge between nums[i] and nums[j] if nums[i] and nums[j] share a common
* factor greater than 1.
*
* Return the size of the largest connected component in the graph.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var largestComponentSize = function(nums) {
const max = Math.max(...nums);
const parent = new Array(max + 1).fill().map((_, i) => i);
const rank = new Array(max + 1).fill(0);
for (const num of nums) {
for (let factor = 2; factor * factor <= num; factor++) {
if (num % factor === 0) {
union(parent, rank, num, factor);
union(parent, rank, num, num / factor);
}
}
}
const map = new Map();
let result = 0;
for (const num of nums) {
const root = find(parent, num);
map.set(root, (map.get(root) || 0) + 1);
result = Math.max(result, map.get(root));
}
return result;
function find(parent, x) {
if (parent[x] !== x) {
parent[x] = find(parent, parent[x]);
}
return parent[x];
}
function union(parent, rank, x, y) {
const rootX = find(parent, x);
const rootY = find(parent, y);
if (rootX !== rootY) {
if (rank[rootX] < rank[rootY]) {
parent[rootX] = rootY;
} else if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
};