当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
【Go】Go 语言切片(Slice)
电压源的电路分析知识分享
DAY18:Xss 靶场通关手册
Bulk free text translation
DirectExchange交换机简单入门demo
毫米波技术基础
熟悉而陌生的新朋友——IAsyncDisposable
电脑开机密码怎么设置?如何给你的电脑加上“安全锁”
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
随机推荐
【Go语言入门教程】Go语言简介
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
【微服务】Nacos集群搭建以及加载文件配置
Bulk free text translation
单点登录 思维导图
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
Database Principles Homework 3 — JMU
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
Introduction and self-order of bcos
服务器和客户端信息的获取
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
安装gstreamer开发依赖库到项目sysroot目录
测试 思维导图
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
芯塔电子斩获第十一届中国双创大赛芜湖赛区桂冠
文件 - 03 下载文件:根据文件id获取下载链接
任务及任务切换
毫米波技术基础
剑指offer(一)
基金投顾业务