LeetCode 952: Largest Component Size by Common Factor Solution
Master LeetCode problem 952 (Largest Component Size by Common Factor), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
952. Largest Component Size by Common Factor
Problem Explanation
Explanation
To solve this problem, we need to first find the common factors of each number in the input array nums. Then, we can create a graph where each node represents a number in nums, and there is an edge between two nodes if their corresponding numbers share a common factor greater than 1.
Next, we perform a depth-first search (DFS) to find the size of the largest connected component in the graph. We keep track of the size of each component using a hashmap. Finally, we return the size of the largest connected component.
-
Algorithm:
- Create a hashmap
parentto store the parent of each number in the input array. - Create a hashmap
sizeto store the size of each component. - Create a function
findto find the parent of a number in the input array. - Create a function
unionto union two components by updating their parent pointers. - Iterate over each number in the input array
nums:- For each number
num, find its factors and iterate over them:- If the factor exists in the
parenthashmap, union the current numbernumand the factor.
- If the factor exists in the
- For each number
- Iterate over each number in the input array
nums:- Find the parent of the number using the
findfunction. - Update the size of the component in the
sizehashmap.
- Find the parent of the number using the
- Find the largest size in the
sizehashmap and return it.
- Create a hashmap
-
Time Complexity: The time complexity of this algorithm is O(n√m * α(n)), where n is the number of elements in the input array
nums, and m is the maximum value innums. α(n) is the inverse Ackermann function, which is very slow-growing and can be considered as constant for practical purposes. -
Space Complexity: The space complexity is O(n) where n is the number of elements in the input array
nums.
Solution Code
class Solution {
public int largestComponentSize(int[] nums) {
Map<Integer, Integer> parent = new HashMap<>();
Map<Integer, Integer> size = new HashMap<>();
for (int num : nums) {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
union(parent, size, num, i);
union(parent, size, num, num / i);
}
}
union(parent, size, num, num);
}
int max = 0;
for (int num : nums) {
int p = find(parent, num);
size.put(p, size.getOrDefault(p, 0) + 1);
max = Math.max(max, size.get(p));
}
return max;
}
private void union(Map<Integer, Integer> parent, Map<Integer, Integer> size, int x, int y) {
int px = find(parent, x);
int py = find(parent, y);
if (px != py) {
parent.put(px, py);
size.put(py, size.getOrDefault(py, 0) + size.getOrDefault(px, 0));
}
}
private int find(Map<Integer, Integer> parent, int x) {
parent.putIfAbsent(x, x);
if (parent.get(x) != x) {
parent.put(x, find(parent, parent.get(x)));
}
return parent.get(x);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 952 (Largest Component Size by Common Factor)?
This page provides optimized solutions for LeetCode problem 952 (Largest Component Size by Common Factor) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 952 (Largest Component Size by Common Factor)?
The time complexity for LeetCode 952 (Largest Component Size by Common Factor) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 952 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 952 in Java, C++, or Python.