标签:prim comm break val app for one isp number
var isPrime[100005] bool
var pa[100005] int
func initPrimeNumbers() {
isPrime[0]=false
isPrime[1]=false
for i:=2;i<=100000;i++ {
isPrime[i]=true
}
for i:=2;i<=100000;i++ {
if(isPrime[i]) {
for j:=i+i; j<=100000; j+=i {
isPrime[j]=false;
}
}
}
}
func find_pa(x int) int {
if(x==pa[x]) {
return x
} else {
pa[x]=find_pa(pa[x])
return pa[x]
}
}
func union_pa(x,y int) {
var pa_x=find_pa(x)
var pa_y=find_pa(y)
if(pa_x!=pa_y) {
pa[pa_x]=pa_y
}
}
func largestComponentSize(A []int) int {
initPrimeNumbers();
primeMap :=[]int{}
for i:=1; i<=100000; i++ {
if(isPrime[i]) {
primeMap=append(primeMap, i)
}
pa[i]=i
}
for _, i := range A {
var tmp=i
for _,prime := range primeMap {
if(prime>tmp) {
break
}
if(tmp%prime==0) {
for tmp%prime==0 {
tmp/=prime
}
union_pa(i,prime)
}
}
}
cntMap:=make(map[int]int)
var maxVal=0
for _, i :=range A {
var idx=find_pa(i)
if _,ok :=cntMap[idx]; ok {
cntMap[idx]+=1
} else {
cntMap[idx]=1
}
if maxVal<cntMap[idx] {
maxVal=cntMap[idx]
}
}
return maxVal
}
「LeetCode」0952-Largest Component Size by Common Factor(Go)
标签:prim comm break val app for one isp number
原文地址:https://www.cnblogs.com/samhx/p/LeetCode-0952.html