Skip to content

3378. Count Connected Components in LCM Graph 👍

  • Time: $O(n\log^* n \cdot \texttt{threshold}))$
  • Space: $O(\texttt{threshold})$
 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
class UnionFind {
 public:
  void unionByRank(int u, int v) {
    const int i = find(u);
    const int j = find(v);
    if (i == j)
      return;
    if (rank[i] < rank[j]) {
      id[i] = j;
    } else if (rank[i] > rank[j]) {
      id[j] = i;
    } else {
      id[i] = j;
      ++rank[j];
    }
  }

  int find(int u) {
    if (!id.contains(u))
      id[u] = u;
    return id[u] == u ? u : id[u] = find(id[u]);
  }

 private:
  unordered_map<int, int> id;
  unordered_map<int, int> rank;
};

class Solution {
 public:
  int countComponents(vector<int>& nums, int threshold) {
    UnionFind uf;

    for (const int num : nums)
      for (int multiple = 2 * num; multiple <= threshold; multiple += num)
        uf.unionByRank(num, multiple);

    unordered_set<int> components;
    for (const int num : nums)
      components.insert(uf.find(num));
    return components.size();
  }
};
 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
class UnionFind {
  public void unionByRank(int u, int v) {
    final int i = find(u);
    final int j = find(v);
    if (i == j)
      return;
    if (rank.get(i) < rank.get(j)) {
      id.put(i, j);
    } else if (rank.get(i) > rank.get(j)) {
      id.put(j, i);
    } else {
      id.put(i, j);
      rank.merge(j, 1, Integer::sum);
    }
  }

  public int find(int u) {
    if (!id.containsKey(u)) {
      id.put(u, u);
      rank.put(u, 0);
    }
    if (id.get(u) != u)
      id.put(u, find(id.get(u)));
    return id.get(u);
  }

  private Map<Integer, Integer> id = new HashMap<>();
  private Map<Integer, Integer> rank = new HashMap<>();
}

class Solution {
  public int countComponents(int[] nums, int threshold) {
    UnionFind uf = new UnionFind();

    for (final int num : nums)
      for (int multiple = 2 * num; multiple <= threshold; multiple += num)
        uf.unionByRank(num, multiple);

    return Arrays.stream(nums).map(uf::find).boxed().collect(Collectors.toSet()).size();
  }
}
 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
class UnionFind:
  def __init__(self):
    self.id = {}
    self.rank = collections.Counter()

  def unionByRank(self, u: int, v: int) -> None:
    i = self.find(u)
    j = self.find(v)
    if i == j:
      return
    if self.rank[i] < self.rank[j]:
      self.id[i] = j
    elif self.rank[i] > self.rank[j]:
      self.id[j] = i
    else:
      self.id[i] = j
      self.rank[j] += 1

  def find(self, u: int) -> int:
    if u not in self.id:
      self.id[u] = u
    if self.id[u] != u:
      self.id[u] = self.find(self.id[u])
    return self.id[u]


class Solution:
  def countComponents(self, nums: list[int], threshold: int) -> int:
    uf = UnionFind()

    for num in nums:
      for multiple in range(2 * num, threshold + 1, num):
        uf.unionByRank(num, multiple)

    return len(set(uf.find(num) for num in nums))