当前位置:网站首页>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;
}
};
边栏推荐
- 湖仓一体电商项目(四):项目数据种类与采集
- These services can't ali interview?Then don't go to, the basic notification, etc
- 推荐系统-排序层:排序层架构【用户、物品特征处理步骤】
- OSS简单上传图片
- Mapped Statements collection does not contain value for的解决方法
- MySQL数据库字段超长问题
- Maxwell 一款简单易上手的实时抓取Mysql数据的软件
- 【请教】SQL语句按列1去重来计算列2之和?
- linux下mysql8安装
- Download Win11 how to change the default path?Download Win11 change the default path method
猜你喜欢

MySQL eight-part text recitation version

MySQL数据库 ---MySQL表的增删改查(进阶)

Is the iPhone really thirteen incense?The two generations of products are completely compared, perhaps the previous generation is more worth buying

Multi-threaded mutex application RAII mechanism

Recommender systems: overview of the characteristics of architecture: user/item engineering -- -- -- -- -- -- -- -- > recall layer > sort layer - > test/evaluation 】 【 cold start problems, real-time 】
![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]

coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products

Download and installation of the latest version of MySQL 8.0 under Linux (detailed steps)

MySQL performance optimization (hardware, system configuration, table structure, SQL statements)

MySQL slow query optimization
随机推荐
These services can't ali interview?Then don't go to, the basic notification, etc
普通的int main(){}没有写return 0;会怎么样?
MySQL database --- Addition, deletion, modification and query of MySQL tables (advanced)
Database indexes: indexes are not a panacea
PostgreSQL 14.4如何安装使用
基于人脸的常见表情识别(2)——数据获取与整理
Trial writing C language sanbang
PHP低代码开发平台 V5.0.7新版发布
MySQL six-pulse sword, SQL customs clearance summary
After MySQL grouping, take the largest piece of data [optimal solution]
MySQL database - views and indexes
“数字化重构系统,搞定 CEO 是第一步”
MySQL database - DQL data query language
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
ELK日志分析系统
ECCV2022 | 对比视觉Transformer的在线持续学习
Difference Between Concurrency and Parallelism
Can't find the distributed lock of Redisson?
vlookup函数匹配不出来只显示公式的解决方法
移动web开发01