|
61 | 61 |
|
62 | 62 | <!-- 这里可写通用的实现逻辑 -->
|
63 | 63 |
|
| 64 | +**方法一:数学 + 并查集** |
| 65 | + |
| 66 | +利用“试除法”,对 $nums$ 中的每个数 $v$ 分解因数,然后将每个因数 $i$ 与 v 合并,$v / i$ 与 $v$ 合并。此过程用并查集来维护连通分量。 |
| 67 | + |
| 68 | +最后,遍历 $nums$ 中每个数 $v$,找出所在的连通分量,出现次数最多的连通分量就是所求的答案。 |
| 69 | + |
64 | 70 | <!-- tabs:start -->
|
65 | 71 |
|
66 | 72 | ### **Python3**
|
67 | 73 |
|
68 | 74 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 75 |
|
70 | 76 | ```python
|
71 |
| - |
| 77 | +class UnionFind: |
| 78 | + def __init__(self, n): |
| 79 | + self.p = list(range(n)) |
| 80 | + |
| 81 | + def union(self, a, b): |
| 82 | + pa, pb = self.find(a), self.find(b) |
| 83 | + if pa != pb: |
| 84 | + self.p[pa] = pb |
| 85 | + |
| 86 | + def find(self, x): |
| 87 | + if self.p[x] != x: |
| 88 | + self.p[x] = self.find(self.p[x]) |
| 89 | + return self.p[x] |
| 90 | + |
| 91 | + |
| 92 | +class Solution: |
| 93 | + def largestComponentSize(self, nums: List[int]) -> int: |
| 94 | + uf = UnionFind(max(nums) + 1) |
| 95 | + for v in nums: |
| 96 | + i = 2 |
| 97 | + while i <= v // i: |
| 98 | + if v % i == 0: |
| 99 | + uf.union(v, i) |
| 100 | + uf.union(v, v // i) |
| 101 | + i += 1 |
| 102 | + return max(Counter(uf.find(v) for v in nums).values()) |
72 | 103 | ```
|
73 | 104 |
|
74 | 105 | ### **Java**
|
75 | 106 |
|
76 | 107 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
77 | 108 |
|
78 | 109 | ```java
|
| 110 | +class UnionFind { |
| 111 | + int[] p; |
| 112 | + |
| 113 | + UnionFind(int n) { |
| 114 | + p = new int[n]; |
| 115 | + for (int i = 0; i < n; ++i) { |
| 116 | + p[i] = i; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + void union(int a, int b) { |
| 121 | + int pa = find(a), pb = find(b); |
| 122 | + if (pa != pb) { |
| 123 | + p[pa] = pb; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + int find(int x) { |
| 128 | + if (p[x] != x) { |
| 129 | + p[x] = find(p[x]); |
| 130 | + } |
| 131 | + return p[x]; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +class Solution { |
| 136 | + public int largestComponentSize(int[] nums) { |
| 137 | + int m = 0; |
| 138 | + for (int v : nums) { |
| 139 | + m = Math.max(m, v); |
| 140 | + } |
| 141 | + UnionFind uf = new UnionFind(m + 1); |
| 142 | + for (int v : nums) { |
| 143 | + int i = 2; |
| 144 | + while (i <= v / i) { |
| 145 | + if (v % i == 0) { |
| 146 | + uf.union(v, i); |
| 147 | + uf.union(v, v / i); |
| 148 | + } |
| 149 | + ++i; |
| 150 | + } |
| 151 | + } |
| 152 | + int[] cnt = new int[m + 1]; |
| 153 | + int ans = 0; |
| 154 | + for (int v : nums) { |
| 155 | + int t = uf.find(v); |
| 156 | + ++cnt[t]; |
| 157 | + ans = Math.max(ans, cnt[t]); |
| 158 | + } |
| 159 | + return ans; |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +### **C++** |
| 165 | + |
| 166 | +```cpp |
| 167 | +class UnionFind { |
| 168 | +public: |
| 169 | + vector<int> p; |
| 170 | + int n; |
| 171 | + |
| 172 | + UnionFind(int _n): n(_n), p(_n) { |
| 173 | + iota(p.begin(), p.end(), 0); |
| 174 | + } |
| 175 | + |
| 176 | + void unite(int a, int b) { |
| 177 | + int pa = find(a), pb = find(b); |
| 178 | + if (pa != pb) p[pa] = pb; |
| 179 | + } |
| 180 | + |
| 181 | + int find(int x) { |
| 182 | + if (p[x] != x) p[x] = find(p[x]); |
| 183 | + return p[x]; |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +class Solution { |
| 188 | +public: |
| 189 | + int largestComponentSize(vector<int>& nums) { |
| 190 | + int m = *max_element(nums.begin(), nums.end()); |
| 191 | + UnionFind* uf = new UnionFind(m + 1); |
| 192 | + for (int v : nums) |
| 193 | + { |
| 194 | + int i = 2; |
| 195 | + while (i <= v / i) |
| 196 | + { |
| 197 | + if (v % i == 0) |
| 198 | + { |
| 199 | + uf->unite(v, i); |
| 200 | + uf->unite(v, v / i); |
| 201 | + } |
| 202 | + ++i; |
| 203 | + } |
| 204 | + } |
| 205 | + vector<int> cnt(m + 1); |
| 206 | + int ans = 0; |
| 207 | + for (int v : nums) |
| 208 | + { |
| 209 | + int t = uf->find(v); |
| 210 | + ++cnt[t]; |
| 211 | + ans = max(ans, cnt[t]); |
| 212 | + } |
| 213 | + return ans; |
| 214 | + } |
| 215 | +}; |
| 216 | +``` |
79 | 217 |
|
| 218 | +### **Go** |
| 219 | +
|
| 220 | +```go |
| 221 | +func largestComponentSize(nums []int) int { |
| 222 | + m := 0 |
| 223 | + for _, v := range nums { |
| 224 | + m = max(m, v) |
| 225 | + } |
| 226 | + p := make([]int, m+1) |
| 227 | + for i := range p { |
| 228 | + p[i] = i |
| 229 | + } |
| 230 | + var find func(int) int |
| 231 | + find = func(x int) int { |
| 232 | + if p[x] != x { |
| 233 | + p[x] = find(p[x]) |
| 234 | + } |
| 235 | + return p[x] |
| 236 | + } |
| 237 | + union := func(a, b int) { |
| 238 | + pa, pb := find(a), find(b) |
| 239 | + if pa != pb { |
| 240 | + p[pa] = pb |
| 241 | + } |
| 242 | + } |
| 243 | + for _, v := range nums { |
| 244 | + i := 2 |
| 245 | + for i <= v/i { |
| 246 | + if v%i == 0 { |
| 247 | + union(v, i) |
| 248 | + union(v, v/i) |
| 249 | + } |
| 250 | + i++ |
| 251 | + } |
| 252 | + } |
| 253 | + ans := 0 |
| 254 | + cnt := make([]int, m+1) |
| 255 | + for _, v := range nums { |
| 256 | + t := find(v) |
| 257 | + cnt[t]++ |
| 258 | + ans = max(ans, cnt[t]) |
| 259 | + } |
| 260 | + return ans |
| 261 | +} |
| 262 | +
|
| 263 | +func max(a, b int) int { |
| 264 | + if a > b { |
| 265 | + return a |
| 266 | + } |
| 267 | + return b |
| 268 | +} |
80 | 269 | ```
|
81 | 270 |
|
82 | 271 | ### **...**
|
|
0 commit comments