当前位置:网站首页>LeetCode 952. Calculate Maximum Component Size by Common Factor
LeetCode 952. Calculate Maximum Component Size by Common Factor
2022-08-03 19:13:00 【Sasakihaise_】
952. By common factorCalculate maximum component size


[Prime factor + Union search] At first, I thought about using gcd to check the common factor. If it is not 1, then use union search and merge, but the complexity is O(n * n * gcd), O(gcd) = O(logm), so obviously it times out.
Another way of thinking, you can find all the divisors of each number, and then use the divisors as the key and the list of numbers as the value to store in the hash table, and merge the numbers under the list with the same key into the same key each time.The complexity of the search set is only O(n * logm), and it is found that nums are different, so it can be discretized to map [1, 10^5] to [1, 2 * 10^4], that is, the storage in the list.The subscript of the array, and the subscript of the merged element in the union search.
The prime factor can be written like this:
for(i = 2; i * i <= n; i++) {
if (n * i== 0) i-> yes;
while (n % i == 0) n /= i; // divide the factor cleanly
}
if(n != 1) n-> yes; // don't forget to add n to the final result at the end
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& nums) {int n = nums.size();f = (int*)malloc(sizeof(int) * N);d = (int*)malloc(sizeof(int) * N);unordered_map> 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 l = it.second;int tmp = l.size();for (auto i = 1; i < tmp; i++) {merge(l[i - 1], l[i]);}}return ans;}}; class Solution {// and lookup set 10:31 13int 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> 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;}} 边栏推荐
- MySQL 主从,6 分钟带你掌握!
- Cobalt Strike (CS) 逆向初探
- Don't look down upon the WebSocket!Long connection, stateful, two-way, full-duplex king is Fried
- G6尝试 学习
- MySQL如何 drop 大表
- if/else或switch替换为Enum
- 【QT】入门心法
- 盲僧发现了华点——教你如何使用API接口获取数据
- Difference差分数组
- Bytes to beat three sides take offer: network + GC + + IO + redis + JVM red-black tree + data structure, to help you quickly into the giant!!!!!
猜你喜欢

BinomialTree 二叉树

要想成为黑客,离不开这十大基础知识

MySQL详细学习教程(建议收藏)

懵逼!阿里一面被虐了,幸获内推华为技术四面,成功拿到offer,年薪40w

Zhong Hua, senior architect of Ali: China-Taiwan strategic thinking and architecture practice; including internal implementation manual

epoll + 线程池 + 前后置服务器分离

awk语法-02-运算、数组、格式化输出

基于DMS的数仓智能运维服务,知多少?

OneNote 教程,如何在 OneNote 中设置页面格式?

余弦距离介绍
随机推荐
云图说丨初识华为云微服务引擎CSE
Solution for no navigation bar after Word is saved as PDF
FreeRTOS中级篇
APT级全面免杀与企业纵深防御体系的红蓝对抗
读取 resources 目录下的文件路径的九种方式,你知道多少?
【WPS-OFFICE-Word】 WPS中样式的运作原理?样式自动更新、自动改变如何处理?样式的管理方法?
typescript学习笔记
阿里巴巴政委体系-第九章、阿里政委启示录
阿里二面:多线程间的通信方式有几种?举例说明
讯方实训云平台——加速教育高质量发展的“数字底座”!
【C语言学习笔记(七)】C语言重定向输入与输出
金鱼哥RHCA回忆录:CL210管理计算资源--管理计算节点+章节实验
Calculation of the array serial number of Likou brush questions (one question per day 7/28)
普通用户如何利用小红书赚钱呢?小红书的流量是真的吗?
【木马免杀】
阿里巴巴政委体系-第八章、阿里政委工作方法论
vulnhub pyexp: 1
idea——同一项目开启多个实例(不同端口)
[Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication
OneNote 教程,如何在 OneNote 中设置页面格式?