deno.land / x / masx200_leetcode_test@10.6.5 / largest-component-size-by-common-factor / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { UnionFind } from "./UnionFind.ts";
function largestComponentSize(nums: number[]) { const uf = new UnionFind(); for (let n of nums) { const temp = n; for (let i = 2; i <= n / i; i++) { let flag = false; while (n % i === 0) { n /= i; flag = true; } if (flag) { uf.union(temp, i); } } if (n > 1) { uf.union(n, temp); } }
const count: Map<number, number> = new Map(); let res = 0; for (const n of nums) { const p = uf.find(n); count.set(p, (count.get(p) ?? 0) + 1); res = Math.max(res, count.get(p) ?? 0); }
return res;}
export default largestComponentSize;
masx200_leetcode_test

Version Info

Tagged at
a year ago