当前位置:网站首页>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;
}
};
边栏推荐
- 测试 思维导图
- interrupt and pendSV
- Analysis of the principle and implementation of waterfall flow layout
- Web浏览器工作流程解析
- Moment.js common methods
- 强化学习科研知识必备(数据库、期刊、会议、牛人)
- 那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
- Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
- 服务器和客户端信息的获取
- 线程中断方法
猜你喜欢

Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary

【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用

Leetcode952. 按公因数计算最大组件大小

那些破釜沉舟入局Web3.0的互联网精英都怎么样了?

文件 - 03 下载文件:根据文件id获取下载链接

基金投顾业务

‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

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

Run the NPM will pop up to ask "how are you going to open this file?"

【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
随机推荐
链表实现及任务调度
Financial leasing business
【科普向】5G核心网架构和关键技术
外贸网站优化-外贸网站优化教程-外贸网站优化软件
DAY18:Xss 靶场通关手册
文件 - 02 上传文件:上传临时文件到服务器
2022.07.20_每日一题
LeetCode刷题——摆动序列#376#Medium
Database Principles Homework 3 — JMU
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
2022.07.13_每日一题
03-SDRAM:写操作(突发)
DirectExchange switch simple introduction demo
嵌入式系统驱动初级【2】——内核模块下_参数和依赖
文件 - 03 下载文件:根据文件id获取下载链接
【Go】Go 语言切片(Slice)
关于求反三角函数的三角函数值
Titanic 预测问题
【第四章】详解Feign的实现原理
从入门到一位合格的爬虫师,这几点很重要