Skip to content

Commit a7732b2

Browse files
committed
0952 added.
1 parent b0ed9e6 commit a7732b2

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(D)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
add_executable(D main.cpp)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/// Source : https://leetcode.com/problems/largest-component-size-by-common-factor/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-12-01
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <unordered_set>
8+
#include <unordered_map>
9+
10+
using namespace std;
11+
12+
13+
/// Using Union Find
14+
/// Time Complexity: O(n * sqrt(max_number))
15+
/// Space Complexity: O(n * sqrt(max_number))
16+
17+
class UF{
18+
19+
private:
20+
vector<int> parent, sz;
21+
22+
public:
23+
UF(int n){
24+
for(int i = 0 ; i < n ; i ++){
25+
parent.push_back(i);
26+
sz.push_back(1);
27+
}
28+
}
29+
30+
int find(int p){
31+
if( p != parent[p] )
32+
parent[p] = find( parent[p] );
33+
return parent[p];
34+
}
35+
36+
bool isConnected(int p , int q){
37+
return find(p) == find(q);
38+
}
39+
40+
void unionElements(int p, int q){
41+
42+
int pRoot = find(p);
43+
int qRoot = find(q);
44+
45+
if( pRoot == qRoot )
46+
return;
47+
48+
parent[pRoot] = qRoot;
49+
sz[qRoot] += sz[pRoot];
50+
}
51+
52+
int size(int p){
53+
return sz[find(p)];
54+
}
55+
};
56+
57+
class Solution {
58+
59+
public:
60+
int largestComponentSize(vector<int>& A) {
61+
62+
unordered_map<int, int> map; // a -> index
63+
for(int i = 0; i < A.size(); i ++)
64+
map[A[i]] = i;
65+
66+
unordered_map<int, vector<int>> factors;
67+
UF uf(A.size());
68+
for(int a: A)
69+
for(int i = 1; i * i <= a; i ++)
70+
if(a % i == 0){
71+
if(i != 1) {
72+
if (!factors[i].empty())
73+
uf.unionElements(map[a], map[factors[i][0]]);
74+
factors[i].push_back(a);
75+
}
76+
if(i * i < a){
77+
int j = a / i;
78+
if(!factors[j].empty())
79+
uf.unionElements(map[a], map[factors[j][0]]);
80+
factors[j].push_back(a);
81+
}
82+
}
83+
84+
int res = 0;
85+
for(int a: A)
86+
res = max(res, uf.size(map[a]));
87+
return res;
88+
}
89+
};
90+
91+
92+
int main() {
93+
94+
vector<int> A = {2,3,6,7,4,12,21,39};
95+
cout << Solution().largestComponentSize(A) << endl;
96+
// 8
97+
98+
return 0;
99+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
608608
| 949 | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits/) | [solution](https://leetcode.com/problems/largest-time-for-given-digits/solution/) | [C++](0949-Largest-Time-for-Given-Digits/cpp-0949/) | | |
609609
| 950 | [Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/) | [solution](https://leetcode.com/problems/reveal-cards-in-increasing-order/solution/) | [C++](0950-Reveal-Cards-In-Increasing-Order/cpp-0950/) | | |
610610
| 951 | [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [solution](https://leetcode.com/problems/flip-equivalent-binary-trees/solution/) | [C++](0951-Flip-Equivalent-Binary-Trees/cpp-0951/) | | |
611+
| 952 | [Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor/) | [solution](https://leetcode.com/problems/largest-component-size-by-common-factor/solution/) | [C++](0952-Largest-Component-Size-by-Common-Factor/cpp-0952/) | | |
611612
| | | | | | |

0 commit comments

Comments
 (0)