当前位置:网站首页>365天挑战LeetCode1000题——Day 044 按公因数计算最大组件大小 并查集
365天挑战LeetCode1000题——Day 044 按公因数计算最大组件大小 并查集
2022-07-30 20:00:00 【ShowM3TheCode】
952. 按公因数计算最大组件大小

代码实现(部分看题解)
这题我想两两求公因数,但是不行,很奇怪,反而是答案看上去笨笨的枚举可以。
class DisjointSet {
private:
std::vector<int> parent;
std::vector<int> rank; // 秩
int maxSetNum;
public:
DisjointSet(int max_size) : parent(std::vector<int>(max_size)), rank(std::vector<int>(max_size, 1)), maxSetNum(1) {
for (int i = 0; i < max_size; ++i) parent[i] = i;
}
int find(int x) {
return x == parent[x] ? x : (parent[x] = find(parent[x]));
}
void _union(int x1, int x2) {
int f1 = find(x1);
int f2 = find(x2);
if (f1 == f2) return;
if (rank[f1] > rank[f2]) {
parent[f2] = f1;
rank[f1] += rank[f2];
maxSetNum = max(maxSetNum, rank[f1]);
}
else {
parent[f1] = f2;
rank[f2] += rank[f1];
maxSetNum = max(maxSetNum, rank[f2]);
}
}
bool is_same(int e1, int e2) {
return find(e1) == find(e2);
}
int getHighestRank() {
return maxSetNum;
}
};
class Solution {
public:
int largestComponentSize(vector<int>& nums) {
int n = *max_element(nums.begin(), nums.end());
DisjointSet ds(n + 1);
for (int num : nums) {
for (int j = 2; j * j <= num; j++) {
if (num % j == 0) {
ds._union(num, num / j);
ds._union(num, j);
}
}
}
vector<int> counts(n + 1);
int root = 0, ans = 0;
for (int num : nums) {
root = ds.find(num);
counts[root]++;
ans = max(ans, counts[root]);
}
return ans;
}
};
边栏推荐
- 【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
- 线性结构:栈和队列
- 如何优化OpenSumi终端性能?
- Scala类中的属性
- These services can't ali interview?Then don't go to, the basic notification, etc
- Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
- excel数字如何转换成文本?excel表格数据转换成文本的方法
- MySQL大批量造数据
- 普通的int main(){}没有写return 0;会怎么样?
- Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
猜你喜欢

excel数字显示e+17怎么恢复?excel数字变成了小数点+E+17的解决方法
![Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]](/img/bb/25b0493398901b52d40ff11a21e34c.png)
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]

vlookup函数匹配不出来只显示公式的解决方法

How to build FTP server under win2003

基于人脸的常见表情识别(1)——深度学习基础知识

网络层协议------IP协议

MySQL slow query optimization

ELK日志分析系统

“数字化重构系统,搞定 CEO 是第一步”

MySQL Functions (Classic Collection)
随机推荐
MySQL数据库字段超长问题
MySQL sub-database sub-table
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
【请教】SQL语句按列1去重来计算列2之和?
How to copy table structure and table data in MySQL
Day31 LeetCode
Linux下载安装mysql5.7版本教程最全详解
利用go制作微信机器人
都在说软件测试没前途,饱和了?为何每年还会增加40万测试员?
Database Tuning - Database Tuning
倾斜文档扫描与字符识别(opencv,坐标变换分析)
To the operation of the int variable assignment is atom?
网络层协议------IP协议
明解C语言第七章习题
centos7安装mysql8
Typora设置标题自动标号
MySQL database master-slave configuration
How to build FTP server under win2003
MySQL复制表结构、表数据的方法