当前位置:网站首页>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;}}
边栏推荐
- Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
- Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
- SQL server 实现触发器备份表数据
- POJ 1465 Multiple(用BFS求能组成的n的最小倍数)
- Word另存为PDF后无导航栏解决办法
- [Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication
- [数据集][VOC]老鼠数据集voc格式3001张
- 【C语言学习笔记(五)】while循环与for循环
- Unity gets the actual coordinates of the ui on the screen under the canvas
- awk语法-02-运算、数组、格式化输出
猜你喜欢
随机推荐
Word另存为PDF后无导航栏解决办法
剑指Offer 56.数组中数字出现的次数
字节跳动三面拿offer:网络+IO+redis+JVM+GC+红黑树+数据结构,助你快速进大厂!!
Rust:多线程并发编程
[笔记]机器学习之前言介绍
红日安全内网渗透靶场-VulnStack-1
网络协议-TCP、UDP区别及TCP三次握手、四次挥手
【统计机器学习】线性回归模型
阿里巴巴政委体系-第七章、阿里政委培育
软件测试技术之如何编写测试用例(3)
Force is brushed buckle problem for the sum of two Numbers
POJ 3041 Asteroids(最大匹配数=最小点覆盖)
Power button brush the topic of merging two orderly array
MySQL如何一劳永逸的永久支持输入中文
BinomialTree 二叉树
京东云发布新一代分布式数据库StarDB 5.0
MD5是对称加密还是非对称加密,有什么优缺点
Postgresql源码(64)查询执行——子模块Executor(2)执行前的数据结构和执行过程
How does MySQL permanently support Chinese input once and for all?
MySQL——增删改查进阶