Skip to content

Commit 839467b

Browse files
committed
test: 952 solution
py, c++, go, java
1 parent 4659b56 commit 839467b

File tree

11 files changed

+521
-3
lines changed

11 files changed

+521
-3
lines changed

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"daily": "687",
2+
"daily": "952",
33
"plans": ["3674", "problems", "3675", "problems", "3676", "problems"]
44
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_687"
4+
problem "leetCode/problems/problems_952"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "687", "problems", problem.Solve)
9+
TestEach(t, "952", "problems", problem.Solve)
1010
}

problems/problems_952/Solution.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
#include <algorithm>
4+
5+
using namespace std;
6+
using json = nlohmann::json;
7+
8+
constexpr int MAX_N = 100000;
9+
array<vector<int>, MAX_N + 1> PRIMES;
10+
11+
bool inited = false;
12+
static void init() {
13+
if (inited) {
14+
return;
15+
}
16+
for (int i = 2; i <= MAX_N; ++i) {
17+
if (PRIMES[i].empty()) {
18+
for (int j = i; j <= MAX_N; j += i) {
19+
PRIMES[j].push_back(i);
20+
}
21+
}
22+
}
23+
}
24+
25+
class UnionFind {
26+
vector<int> fa;
27+
vector<int> size;
28+
29+
public:
30+
int cc;
31+
explicit UnionFind(int n) : fa(n), size(n, 1), cc(n) {
32+
for (int i = 0; i < n; i++) {
33+
fa[i] = i;
34+
}
35+
}
36+
37+
int find(int x) {
38+
if (fa[x] != x) {
39+
fa[x] = find(fa[x]);
40+
}
41+
return fa[x];
42+
}
43+
44+
bool merge(int x, int y) {
45+
int px = find(x), py = find(y);
46+
if (px == py) {
47+
return false;
48+
}
49+
fa[px] = py;
50+
size[py] += size[px];
51+
cc--;
52+
return true;
53+
}
54+
55+
int get_size(int x) { return size[find(x)]; }
56+
};
57+
58+
class Solution {
59+
public:
60+
int largestComponentSize(const vector<int> &nums) {
61+
init();
62+
int n = nums.size();
63+
UnionFind uf(n);
64+
unordered_map<int, int> primes_idx;
65+
for (int i = 0; i < n; ++i) {
66+
for (int p : PRIMES[nums[i]]) {
67+
auto it = primes_idx.find(p);
68+
if (it != primes_idx.end()) {
69+
uf.merge(i, it->second);
70+
}
71+
primes_idx[p] = i;
72+
}
73+
}
74+
int ans = 0;
75+
for (int i = 0; i < n; ++i) {
76+
ans = max(ans, uf.get_size(i));
77+
}
78+
return ans;
79+
}
80+
};
81+
82+
json leetcode::qubh::Solve(string input_json_values) {
83+
vector<string> inputArray;
84+
size_t pos = input_json_values.find('\n');
85+
while (pos != string::npos) {
86+
inputArray.push_back(input_json_values.substr(0, pos));
87+
input_json_values = input_json_values.substr(pos + 1);
88+
pos = input_json_values.find('\n');
89+
}
90+
inputArray.push_back(input_json_values);
91+
92+
Solution solution;
93+
vector<int> nums = json::parse(inputArray.at(0));
94+
return solution.largestComponentSize(nums);
95+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package problems.problems_952;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import java.util.*;
5+
import qubhjava.BaseSolution;
6+
7+
8+
public class Solution extends BaseSolution {
9+
10+
class UnionFind {
11+
private int[] parent;
12+
private int[] size;
13+
private int count;
14+
15+
public UnionFind(int n) {
16+
parent = new int[n];
17+
size = new int[n];
18+
count = n;
19+
for (int i = 0; i < n; i++) {
20+
parent[i] = i;
21+
size[i] = 1;
22+
}
23+
}
24+
25+
public int find(int x) {
26+
if (parent[x] != x) {
27+
parent[x] = find(parent[x]); // Path compression
28+
}
29+
return parent[x];
30+
}
31+
32+
public boolean union(int x, int y) {
33+
int px = find(x);
34+
int py = find(y);
35+
if (px == py) {
36+
return false; // Already in the same set
37+
}
38+
if (size[px] < size[py]) {
39+
parent[px] = py;
40+
size[py] += size[px];
41+
} else {
42+
parent[py] = px;
43+
size[px] += size[py];
44+
}
45+
count--;
46+
return true; // Union successful
47+
}
48+
49+
public int getCount() {
50+
return count;
51+
}
52+
53+
public int getSize(int x) {
54+
return size[find(x)];
55+
}
56+
}
57+
private static final int MAX_N = 100000;
58+
private static List<Integer>[] PRIMES = new List[MAX_N + 1];
59+
static {
60+
for (int i = 0; i <= MAX_N; ++i) {
61+
PRIMES[i] = new ArrayList<>();
62+
}
63+
for (int i = 2; i <= MAX_N; ++i) {
64+
if (PRIMES[i].isEmpty()) {
65+
for (int j = i; j <= MAX_N; j += i) {
66+
PRIMES[j].add(i);
67+
}
68+
}
69+
}
70+
}
71+
public int largestComponentSize(int[] nums) {
72+
int n = nums.length;
73+
UnionFind uf = new UnionFind(n);
74+
Map<Integer, Integer> primeIdx = new HashMap<>();
75+
for (int i = 0; i < n; ++i) {
76+
for (int p: PRIMES[nums[i]]) {
77+
if (primeIdx.containsKey(p)) {
78+
uf.union(primeIdx.get(p), i);
79+
}
80+
primeIdx.put(p, i);
81+
}
82+
}
83+
int ans = 0;
84+
for (int i = 0; i < n; ++i) {
85+
ans = Math.max(ans, uf.getSize(i));
86+
}
87+
return ans;
88+
}
89+
90+
@Override
91+
public Object solve(String[] inputJsonValues) {
92+
int[] nums = jsonArrayToIntArray(inputJsonValues[0]);
93+
return JSON.toJSON(largestComponentSize(nums));
94+
}
95+
}

problems/problems_952/problem.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 952. Largest Component Size by Common Factor [Rating: 2272.11]
2+
3+
<p>You are given an integer array of unique positive integers <code>nums</code>. Consider the following graph:</p>
4+
5+
<ul>
6+
<li>There are <code>nums.length</code> nodes, labeled <code>nums[0]</code> to <code>nums[nums.length - 1]</code>,</li>
7+
<li>There is an undirected edge between <code>nums[i]</code> and <code>nums[j]</code> if <code>nums[i]</code> and <code>nums[j]</code> share a common factor greater than <code>1</code>.</li>
8+
</ul>
9+
10+
<p>Return <em>the size of the largest connected component in the graph</em>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/01/ex1.png" style="width: 500px; height: 97px;" />
15+
<pre>
16+
<strong>Input:</strong> nums = [4,6,15,35]
17+
<strong>Output:</strong> 4
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/01/ex2.png" style="width: 500px; height: 85px;" />
22+
<pre>
23+
<strong>Input:</strong> nums = [20,50,9,63]
24+
<strong>Output:</strong> 2
25+
</pre>
26+
27+
<p><strong class="example">Example 3:</strong></p>
28+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/01/ex3.png" style="width: 500px; height: 260px;" />
29+
<pre>
30+
<strong>Input:</strong> nums = [2,3,6,7,4,12,21,39]
31+
<strong>Output:</strong> 8
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
39+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
40+
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
41+
</ul>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 952. 按公因数计算最大组件大小 [难度分: 2272.11]
2+
3+
<p>给定一个由不同正整数的组成的非空数组&nbsp;<code>nums</code> ,考虑下面的图:</p>
4+
5+
<ul>
6+
<li>有&nbsp;<code>nums.length</code>&nbsp;个节点,按从&nbsp;<code>nums[0]</code>&nbsp;到&nbsp;<code>nums[nums.length - 1]</code>&nbsp;标记;</li>
7+
<li>只有当&nbsp;<code>nums[i]</code>&nbsp;和&nbsp;<code>nums[j]</code>&nbsp;共用一个大于 1 的公因数时,<code>nums[i]</code>&nbsp;和&nbsp;<code>nums[j]</code>之间才有一条边。</li>
8+
</ul>
9+
10+
<p>返回 <em>图中最大连通组件的大小</em> 。</p>
11+
12+
<p>&nbsp;</p>
13+
14+
<ol>
15+
</ol>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<p><img src="https://assets.leetcode.com/uploads/2018/12/01/ex1.png" style="height: 97px; width: 500px;" /></p>
20+
21+
<pre>
22+
<strong>输入:</strong>nums = [4,6,15,35]
23+
<strong>输出:</strong>4
24+
</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
28+
<p><img src="https://assets.leetcode.com/uploads/2018/12/01/ex2.png" style="height: 85px; width: 500px;" /></p>
29+
30+
<pre>
31+
<strong>输入:</strong>nums = [20,50,9,63]
32+
<strong>输出:</strong>2
33+
</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<p><img src="https://assets.leetcode.com/uploads/2018/12/01/ex3.png" style="height: 260px; width: 500px;" /></p>
38+
39+
<pre>
40+
<strong>输入:</strong>nums = [2,3,6,7,4,12,21,39]
41+
<strong>输出:</strong>8
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
50+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
51+
<li><code>nums</code>&nbsp;中所有值都 <strong>不同</strong></li>
52+
</ul>

0 commit comments

Comments
 (0)