-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
292 lines (248 loc) · 7.01 KB
/
main.cpp
File metadata and controls
292 lines (248 loc) · 7.01 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Source: https://leetcode.com/problems/largest-component-size-by-common-factor
// Title: Largest Component Size by Common Factor
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given an integer array of unique positive integers `nums`. Consider the following graph:
//
// - There are `nums.length` nodes, labeled `nums[0]` to `nums[nums.length - 1]`,
// - There is an undirected edge between `nums[i]` and `nums[j]` if `nums[i]` and `nums[j]` share a common factor greater than `1`.
//
// Return the size of the largest connected component in the graph.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2018/12/01/ex1.png
//
// ```
// Input: nums = [4,6,15,35]
// Output: 4
// ```
//
// **Example 2:**
// https://assets.leetcode.com/uploads/2018/12/01/ex2.png
//
// ```
// Input: nums = [20,50,9,63]
// Output: 2
// ```
//
// **Example 3:**
// https://assets.leetcode.com/uploads/2018/12/01/ex3.png
//
// ```
// Input: nums = [2,3,6,7,4,12,21,39]
// Output: 8
// ```
//
// **Constraints:**
//
// - `1 <= nums.length <= 2 * 10^4`
// - `1 <= nums[i] <= 10^5`
// - All the values of `nums` are **unique**.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <concepts>
#include <functional>
#include <numeric>
#include <vector>
using namespace std;
// Union Find + GCD
//
// Loop through all pairs of numbers.
// For each pair, skip if the they already in the same group.
// Otherwise, compute the GCD to check if they need to merge.
class Solution {
struct UnionFind {
vector<int> parents;
vector<int> sizes; // rank is faster, but we need size here.
UnionFind(const int n) : parents(n), sizes(n, 1) { //
iota(parents.begin(), parents.end(), 0);
}
int find(const int x) {
if (parents[x] != x) {
parents[x] = find(parents[x]);
}
return parents[x];
}
bool isConnected(const int x, const int y) { //
return find(x) == find(y);
}
void uniteRoot(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
// Ensure size(x) >= size(y)
if (sizes[x] < sizes[y]) swap(x, y);
// Merge y into x
sizes[x] += sizes[y];
parents[y] = x;
}
};
public:
int largestComponentSize(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0;
// Union Find
auto uf = UnionFind(n);
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (uf.isConnected(i, j)) continue;
if (gcd(nums[i], nums[j]) == 1) continue;
uf.uniteRoot(i, j);
}
}
auto maxIt = max_element(uf.sizes.cbegin(), uf.sizes.cend());
return *maxIt; // uf.sizes is not empty
}
};
// Union Find + Prime Factorization
//
// Find the prime factors of each number. (O(sqrt(M)), M is maximum number)
// Create a set of each prime, and put the numbers into the sets of its prime factors.
//
// For each prime, union all numbers inside its set using union find.
class Solution2 {
struct UnionFind {
vector<int> parents;
vector<int> sizes; // rank is faster, but we need size here.
UnionFind(const int n) : parents(n), sizes(n, 1) { //
iota(parents.begin(), parents.end(), 0);
}
int find(const int x) {
if (parents[x] != x) {
parents[x] = find(parents[x]);
}
return parents[x];
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
// Ensure size(x) >= size(y)
if (sizes[x] < sizes[y]) swap(x, y);
// Merge y into x
sizes[x] += sizes[y];
parents[y] = x;
}
};
// TODO: use generator instead of vector
vector<int> getPrimeFactors(const int num) {
auto factors = vector<int>();
int quotient = num;
// p = 2
if (quotient % 2 == 0) {
factors.push_back(2);
do {
quotient /= 2;
} while (quotient % 2 == 0);
}
// try all odd number
for (int p = 3; p * p <= quotient; p += 2) {
if (quotient % p == 0) {
factors.push_back(p);
do {
quotient /= p;
} while (quotient % p == 0);
}
}
if (quotient > 1) factors.push_back(quotient);
return factors;
}
public:
int largestComponentSize(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0;
// Group numbers by its factors
auto primeSets = unordered_map<int, vector<int>>(); // prime -> ID of multiples
for (int i = 0; i < n; ++i) {
for (auto p : getPrimeFactors(nums[i])) {
primeSets[p].push_back(i);
}
}
// Union Find
auto uf = UnionFind(n);
for (auto& [_, ids] : primeSets) {
for (auto id : ids) {
uf.unite(ids[0], id);
}
}
auto maxIt = max_element(uf.sizes.cbegin(), uf.sizes.cend());
return *maxIt; // uf.sizes is not empty
}
};
// Union Find + Prime Factorization + Iterator
//
// Find the prime factors of each number. (O(sqrt(M)), M is maximum number)
// Create a set of each prime, and put the numbers into the sets of its prime factors.
//
// For each prime, union all numbers inside its set using union find.
class Solution3 {
struct UnionFind {
vector<int> parents;
vector<int> sizes; // rank is faster, but we need size here.
UnionFind(const int n) : parents(n), sizes(n, 1) { //
iota(parents.begin(), parents.end(), 0);
}
int find(const int x) {
if (parents[x] != x) {
parents[x] = find(parents[x]);
}
return parents[x];
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
// Ensure size(x) >= size(y)
if (sizes[x] < sizes[y]) swap(x, y);
// Merge y into x
sizes[x] += sizes[y];
parents[y] = x;
}
};
// yield is void(int)
void getPrimeFactors(const int num, invocable<int> auto yield) {
int quotient = num;
// p = 2
if (quotient % 2 == 0) {
yield(2);
do {
quotient /= 2;
} while (quotient % 2 == 0);
}
// try all odd number
for (int p = 3; p * p <= quotient; p += 2) {
if (quotient % p == 0) {
yield(p);
do {
quotient /= p;
} while (quotient % p == 0);
}
}
if (quotient > 1) {
yield(quotient);
}
}
public:
int largestComponentSize(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0;
// Group numbers by its factors
auto primeSets = unordered_map<int, vector<int>>(); // prime -> ID of multiples
for (int i = 0; i < n; ++i) {
getPrimeFactors(nums[i], [&](int p) { //
primeSets[p].push_back(i);
});
}
// Union Find
auto uf = UnionFind(n);
for (auto& [_, ids] : primeSets) {
for (auto id : ids) {
uf.unite(ids[0], id);
}
}
auto maxIt = max_element(uf.sizes.cbegin(), uf.sizes.cend());
return *maxIt; // uf.sizes is not empty
}
};