当前位置:网站首页>Leetcode952. Calculate maximum component size by common factor
Leetcode952. Calculate maximum component size by common factor
2022-07-31 07:39:00 【hhhcbw】
题目链接
题目大意

比如 nums = [4,6,15,35] 答案就是4,nums = [20,50,9,63] 答案就是2.
解题思路
我的思路是对 nums Prime factorization of each number in the array,Then a union can be maintained for each factor,For a number, all factors after its prime factorization can be regarded as a connected set.In this way, you can maintain and check the set size online.素数筛+质因子分解+并查集,时间复杂度为 O(mlogn),m为数组大小,n为数字大小.
当然,You can also follow the official solution,Direct use and query set maintenance,Every number and every factor is a connected component,Finally, determine who the father of the current number is,And just update the set size.The official time complexity is O(m n \sqrt{n} n).
代码(C++)
我的代码
const int N = 1e5+5;
class Solution {
public:
//核心:n只会被最小质因子筛掉
int cnt = 0; //素数的个数
int v[N]; //Store the smallest prime factor of each number
int prime[N]; //存素数
int fa[N]; // 并查集父节点
int size[N]; // 并查集大小
void get_primes(int n){
for(int i = 2;i <= n; ++i){
if(v[i] == 0){
v[i] = i;
prime[++cnt] = i;
}
for(int j = 1 ; j <= cnt;++j){
if(prime[j] > v[i] || i * prime[j] > n) break;
v[prime[j] * i] = prime[j];
}
}
}
// void divide(int n) {
// while (v[n]) {
// printf("%d ", v[n]); // 在前
// n /= v[n]; //在后
// }
// }
int find(int x)
{
if(x == fa[x])
return x;
else
return find(fa[x]);
}
int largestComponentSize(vector<int>& nums) {
int ans = 1;
get_primes(100000);
for(int i=0; i<nums.size(); ++i)
{
int father = 0;
while (v[nums[i]])
{
int now = v[nums[i]];
if(father == 0)
{
father = now;
if(fa[father] == 0)
{
fa[father] = father;
size[father] = 1;
}
else
{
size[find(father)] ++;
ans = max(ans, size[find(father)]);
}
}
else
{
if(fa[now] == 0)
{
fa[now] = find(father);
}
else if(find(father) != find(now))
{
size[find(father)] += size[find(now)];
ans = max(size[find(father)], ans);
fa[find(now)] = find(father);
}
}
while (nums[i] % now == 0)
nums[i] /= now;
}
}
return ans;
}
};
官方代码
class UnionFind {
public:
UnionFind(int n) {
parent = vector<int>(n);
rank = vector<int>(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
void uni(int x, int y) {
int rootx = find(x);
int rooty = find(y);
if (rootx != rooty) {
if (rank[rootx] > rank[rooty]) {
parent[rooty] = rootx;
} else if (rank[rootx] < rank[rooty]) {
parent[rootx] = rooty;
} else {
parent[rooty] = rootx;
rank[rootx]++;
}
}
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
private:
vector<int> parent;
vector<int> rank;
};
class Solution {
public:
int largestComponentSize(vector<int>& nums) {
int m = *max_element(nums.begin(), nums.end());
UnionFind uf(m + 1);
for (int num : nums) {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
uf.uni(num, i);
uf.uni(num, num / i);
}
}
}
vector<int> counts(m + 1);
int ans = 0;
for (int num : nums) {
int root = uf.find(num);
counts[root]++;
ans = max(ans, counts[root]);
}
return ans;
}
};
边栏推荐
猜你喜欢

03-SDRAM: Write operation (burst)

自动翻译软件-批量批量自动翻译软件推荐

Titanic 预测问题

postgresql源码学习(34)—— 事务日志⑩ - 全页写机制

【面试:并发篇37:多线程:线程池】自定义线程池

shell之条件语句(test、if、case)

零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

2022.07.18_每日一题

Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

【Go语言入门教程】Go语言简介
随机推荐
事务的四大特性
Redux state management
Web浏览器工作流程解析
SQL Server Datetime2数据类型
R——避免使用 col=0
2022.07.13_每日一题
Analysis of the principle and implementation of waterfall flow layout
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
从入门到一位合格的爬虫师,这几点很重要
Kubernetes scheduling
LeetCode brush # 376 # Medium - swing sequence
从 Google 离职,前Go 语言负责人跳槽小公司
毫米波技术基础
把 VS Code 当游戏机
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
SCI写作指南
Third-party library-store
Log4net 思维导图
基于LSTM的诗词生成