当前位置:网站首页>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;
}
};
边栏推荐
- 基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
- Third-party library-store
- Introduction and self-order of bcos
- 深度学习通信领域相关经典论文、数据集整理分享
- 文件 - 04 下载文件: 根据文件下载链接下载文件
- 【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
- 第9章 异常try...except...else...finally
- 强化学习科研知识必备(数据库、期刊、会议、牛人)
- 任务及任务切换
- Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
猜你喜欢
360 push-360 push tool-360 batch push tool
电脑开机密码怎么设置?如何给你的电脑加上“安全锁”
Difficulty comparison between high concurrency and multithreading (easy to confuse)
One of the small practical projects - food alliance ordering system
关于求反三角函数的三角函数值
Log4net 思维导图
2022.07.24_每日一题
电压源的电路分析知识分享
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
从 Google 离职,前Go 语言负责人跳槽小公司
随机推荐
Log4net 思维导图
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
【Star项目】小帽飞机大战(七)
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
Gradle remove dependency demo
QFileInfo常规方法
嵌入式系统驱动初级【2】——内核模块下_参数和依赖
Difficulty comparison between high concurrency and multithreading (easy to confuse)
电脑开机密码怎么设置?如何给你的电脑加上“安全锁”
Introduction and self-order of bcos
nohup principle
电压源的电路分析知识分享
服务器和客户端信息的获取
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
R——避免使用 col=0
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
熟悉而陌生的新朋友——IAsyncDisposable
SQLite数据库连接字符串
Database Principles Homework 3 — JMU
文件 - 05 下载文件:根据文件Id下载文件