当前位置:网站首页>LeetCode 952. 按公因数计算最大组件大小
LeetCode 952. 按公因数计算最大组件大小
2022-08-03 19:03:00 【Sasakihaise_】
【质因数+并查集】一开始我想的是两两用gcd查公因数,如果不是1就用并查集合并,但是复杂度O(n * n * gcd),O(gcd) = O(logm),所以显然是超时的。
换种思路,可以求每个数的所有约数,然后以约数为key,以数的list为value存到哈希表中,每次把相同key的list下的数合并到并查集中这样复杂度只有O(n * logm),另外发现nums各不相同因此可以进行离散化把[1, 10^5]映射到[1, 2 * 10^4]上,也就是list中存数组的下标,并查集中也是合并元素下标。
求质因数可以这么写:
for(i = 2; i * i <= n; i++) {
if (n * i == 0) i-> yes;
while (n % i == 0) n /= i; // 把这个因数除干净
}
if(n != 1) n-> yes; //最后千万别忘了把最终得到的n加上
class Solution {
public:
int N = (int)2e4 + 1;
int *f;
int *d;
int ans = 1;
int ask(int x) {
return x == f[x]? x: ask(f[x]);
}
void merge(int x, int y) {
x = ask(x);
y = ask(y);
if (x == y) return;
d[x] += d[y];
f[y] = f[x];
ans = max(d[x], ans);
}
int largestComponentSize(vector<int>& nums) {
int n = nums.size();
f = (int*)malloc(sizeof(int) * N);
d = (int*)malloc(sizeof(int) * N);
unordered_map<int, vector<int>> m;
for (auto i = 0; i < N; i++) f[i] = i;
for (auto i = 0; i < N; i++) d[i] = 1;
for (auto i = 0; i < n; i++) {
int x = nums[i];
for (auto j = 2; j * j <= x; j++) {
if (x % j == 0) m[j].push_back(i);
while (x % j == 0) x /= j;
}
if (x != 1) m[x].push_back(i);
}
for (auto & it: m) {
vector<int> l = it.second;
int tmp = l.size();
for (auto i = 1; i < tmp; i++) {
merge(l[i - 1], l[i]);
}
}
return ans;
}
};
class Solution {
// 并查集 10:31 13
int N = 20001;
int[] f = new int[N];
int[] cnt = new int[N];
int ans = 1;
int gcd(int a, int b) {
return a % b == 0? b: gcd(b, a % b);
}
int ask(int x) {
return x == f[x]? x: ask(f[x]);
}
void union(int x, int y) {
x = ask(x); y = ask(y);
if (x == y) return;
cnt[x] += cnt[y];
f[y] = x;
ans = Math.max(ans, cnt[x]);
}
public int largestComponentSize(int[] nums) {
for (var i = 0; i < N; i++) f[i] = i;
Arrays.fill(cnt, 1);
int n = nums.length;
Map<Integer, List<Integer>> map = new HashMap<>();
for (var i = 0; i < n; ++i) {
var x = nums[i];
for (var j = 2; j * j <= x; j++) {
if (x % j == 0) {
map.computeIfAbsent(j, k -> new ArrayList()).add(i);
}
while (x % j == 0) x /= j;
}
if (x != 1)
map.computeIfAbsent(x, k -> new ArrayList()).add(i);
}
for (var k: map.keySet()) {
var list = map.get(k);
int m = list.size();
for (var i = 1; i < m; i++) {
union(list.get(i - 1), list.get(i));
}
}
return ans;
}
}
边栏推荐
猜你喜欢
[Notes] Introduction to machine learning
国产虚拟化云宏CNware WinStack安装体验-5 开启集群HA
学弟:我适不适合转行做软件测试?
BinomialTree 二叉树
要想成为黑客,离不开这十大基础知识
设备树基本原理与操作方法
How does MySQL permanently support Chinese input once and for all?
Bytes to beat three sides take offer: network + GC + + IO + redis + JVM red-black tree + data structure, to help you quickly into the giant!!!!!
[Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication
系统太多,多账号互通如何实现?
随机推荐
fatal error: jni.h: No such file or directory
MySQL读写分离的三种实现方案
字节跳动三面拿offer:网络+IO+redis+JVM+GC+红黑树+数据结构,助你快速进大厂!!
丙二醇二乙酸酯(Propylene Glycol Diacetate)
dd命令:用于读取、转换并输出数据
【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position
一文搞懂│php 中的 DI 依赖注入
if/else或switch替换为Enum
Shell编程案例
ctfshow php特性
BinomialTree 二叉树
MySQL 啥时候用表锁,啥时候用行锁?这些你都应该知道吧
warnings.warn(“Title is more than 31 characters. Some applications may not be able to read the file
PreFixSum前缀和
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
力扣刷题之分数加减运算(每日一题7/27)
梅科尔工作室-14天华为培训六
阿里巴巴政委体系-第五章、阿里政委体系建设
Install porterLB
阿里资深架构师钟华曰:中台战略思想与架构实战;含内部实施手册