当前位置:网站首页>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;}} 边栏推荐
猜你喜欢

When does MySQL use table locks and when to use row locks?You should know this

LOL英雄联盟卡顿掉帧问题解决办法 2022年8月1日

CS免杀姿势

面试突击:什么是粘包和半包?怎么解决?

国产虚拟化云宏CNware WinStack安装体验-5 开启集群HA

How does MySQL permanently support Chinese input once and for all?

京东云发布新一代分布式数据库StarDB 5.0

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

WEB 渗透之CSRF

如何理解即时通讯开发移动网络的“弱”和“慢”
随机推荐
pg_memory_barrier_impl in Postgresql and C's volatile
Unity获取canvas 下ui 在屏幕中的实际坐标
[Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication
学弟:我适不适合转行做软件测试?
力扣刷题之有效的正方形(每日一题7/29)
unity3d-游戏物体控制方法
阿里资深专家打造从零开始学架构,含阿里内部技术栈PPT、PFD实战
CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
【WPS-OFFICE-Word】 WPS中样式的运作原理?样式自动更新、自动改变如何处理?样式的管理方法?
C#将位图旋转90度
Postgresql snapshot optimization Globalvis new system analysis (performance greatly enhanced)
Handler 源码解析
Postgresql源码(64)查询执行——子模块Executor(2)执行前的数据结构和执行过程
如何理解即时通讯开发移动网络的“弱”和“慢”
pytest接口自动化测试框架 | 基于Pytest的Web UI自动化测试框架介绍
[数据集][VOC]老鼠数据集voc格式3001张
WEB 渗透之CSRF
Postgresql-xl global snapshot and GTM code walking (branch line)
揭秘5名运维如何轻松管理数亿级流量系统
梅科尔工作室-14天华为培训七