当前位置:网站首页>leetcode:952. 按公因数计算最大组件大小【并查集】
leetcode:952. 按公因数计算最大组件大小【并查集】
2022-07-29 19:40:00 【白速龙王的回眸】

分析
一个数,它和它所有的因数都应该是一组
然后对所有数都这样分解
最后,对所有数,可以得到root
这个root就是对应的分组
ac code
class Solution:
def largestComponentSize(self, nums: List[int]) -> int:
n = len(nums)
parent = list(range(10 ** 5 + 10))
def find(index: int) -> int:
if parent[index] != index:
parent[index] = find(parent[index])
return parent[index]
def union(index1: int, index2: int):
# 后者作为爸爸
parent[find(index1)] = find(index2)
for num in nums:
upper_bound = int(sqrt(num))
# core code
for i in range(2, upper_bound + 1):
if num % i == 0:
union(num, i)
union(num, num // i)
ans = 1
cnt = [0] * (10 ** 5 + 10)
for num in nums:
root = find(num)
cnt[root] += 1
ans = max(ans, cnt[root])
return ans
总结
简单并查集应用
边栏推荐
猜你喜欢

ds1302——Bin brother 51

悲伤的摇滚乐

接口测试工具之Postman详解

H264码流RTP封装方式详解
![[mathematical foundation] probability and mathematical statistics related concept learning](/img/bc/d3a246240ff7aca2b84c3766383758.png)
[mathematical foundation] probability and mathematical statistics related concept learning

Typescript mix method to class with decorator

Flink1.15源码阅读flink-clients之GenericCLI、flinkYarnSessionCLI和DefaultCLI

error TS1219: Experimental support for decorators解决

【AutoSAR 九 C/S原理架构】

【体系结构 四 存储结构】
随机推荐
Typescript使用修饰器混合方法到类
2.5w字 + 36 张图爆肝操作系统面试题 学不吐你
私域增长 | 私域会员:9大连锁行业15个案例集锦
基于STM32的RFID-RC522门禁系统
PostgreSQL语句大全
updatexml、extractvalue和floor报错注入原理
sad rock
etcd implements large-scale service governance application combat
scratch 编程 + 小学数学
yarn的安装和使用(yarn安装mysql)
[数学基础]概率论与数理统计相关概念学习
【AutoSAR 八 OS】
cv2 imread()函数[通俗易懂]
【AutoSAR 十三 NVM】
TDengine 落地协鑫能科,数百亿数据压缩至 600GB
exdark数据集转yolo格式(仅供参考)
直播预约 | 如何通过MLOps解放和提升AI生产力?
FPGA设计8位异步、同步二进制计数器
12437字,带你深入探究RPC通讯原理
LeetCode #88.合并两个有序数组