Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit a1b6db3

Browse files
committed
952 added
1 parent 5a41d25 commit a1b6db3

File tree

7 files changed

+116
-23
lines changed

7 files changed

+116
-23
lines changed
Loading
Loading
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# [952. Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor/)
2+
3+
Given a non-empty array of unique positive integers `A`, consider the following graph:
4+
5+
- There are `A.length` nodes, labelled `A[0]` to `A[A.length - 1]`;
6+
- There is an edge between `A[i]` and `A[j]` if and only if `A[i]` and `A[j]` share a common factor greater than 1.
7+
8+
Return the size of the largest connected component in the graph.
9+
10+
Example 1:
11+
12+
```text
13+
Input: [4,6,15,35]
14+
Output: 4
15+
```
16+
17+
![example1](1.png)
18+
19+
Example 2:
20+
21+
```text
22+
Input: [20,50,9,63]
23+
Output: 2
24+
```
25+
26+
![example2](2.png)
27+
28+
Example 3:
29+
30+
```text
31+
Input: [2,3,6,7,4,12,21,39]
32+
Output: 8
33+
```
34+
35+
![example3](3.png)
36+
37+
Note:
38+
39+
1. `1 <= A.length <= 20000`
40+
1. `1 <= A[i] <= 100000`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem0952
2+
3+
func largestComponentSize(A []int) int {
4+
5+
return 0
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package problem0952
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
A []int
12+
ans int
13+
}{
14+
15+
{
16+
[]int{4, 6, 15, 35},
17+
4,
18+
},
19+
20+
{
21+
[]int{20, 50, 9, 63},
22+
2,
23+
},
24+
25+
{
26+
[]int{2, 3, 6, 7, 4, 12, 21, 39},
27+
8,
28+
},
29+
30+
// 可以有多个 testcase
31+
}
32+
33+
func Test_largestComponentSize(t *testing.T) {
34+
ast := assert.New(t)
35+
36+
for _, tc := range tcs {
37+
ast.Equal(tc.ans, largestComponentSize(tc.A), "输入:%v", tc)
38+
}
39+
}
40+
41+
func Benchmark_largestComponentSize(b *testing.B) {
42+
for i := 0; i < b.N; i++ {
43+
for _, tc := range tcs {
44+
largestComponentSize(tc.A)
45+
}
46+
}
47+
}

leetcode.json

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 903,
4-
"Updated": "2019-02-06T12:54:02.888667396+08:00",
4+
"Updated": "2019-02-07T12:28:30.824001131+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 211,
@@ -1057,7 +1057,7 @@
10571057
"ID": 86,
10581058
"Title": "Partition List",
10591059
"TitleSlug": "partition-list",
1060-
"PassRate": "35%",
1060+
"PassRate": "36%",
10611061
"Difficulty": "Medium",
10621062
"IsAccepted": true,
10631063
"IsPaid": false,
@@ -1693,7 +1693,7 @@
16931693
"ID": 139,
16941694
"Title": "Word Break",
16951695
"TitleSlug": "word-break",
1696-
"PassRate": "33%",
1696+
"PassRate": "34%",
16971697
"Difficulty": "Medium",
16981698
"IsAccepted": true,
16991699
"IsPaid": false,
@@ -2941,7 +2941,7 @@
29412941
"ID": 243,
29422942
"Title": "Shortest Word Distance",
29432943
"TitleSlug": "shortest-word-distance",
2944-
"PassRate": "55%",
2944+
"PassRate": "56%",
29452945
"Difficulty": "Easy",
29462946
"IsAccepted": false,
29472947
"IsPaid": true,
@@ -3973,7 +3973,7 @@
39733973
"ID": 329,
39743974
"Title": "Longest Increasing Path in a Matrix",
39753975
"TitleSlug": "longest-increasing-path-in-a-matrix",
3976-
"PassRate": "38%",
3976+
"PassRate": "39%",
39773977
"Difficulty": "Hard",
39783978
"IsAccepted": true,
39793979
"IsPaid": false,
@@ -5581,7 +5581,7 @@
55815581
"ID": 463,
55825582
"Title": "Island Perimeter",
55835583
"TitleSlug": "island-perimeter",
5584-
"PassRate": "59%",
5584+
"PassRate": "60%",
55855585
"Difficulty": "Easy",
55865586
"IsAccepted": true,
55875587
"IsPaid": false,
@@ -6145,7 +6145,7 @@
61456145
"ID": 510,
61466146
"Title": "Inorder Successor in BST II",
61476147
"TitleSlug": "inorder-successor-in-bst-ii",
6148-
"PassRate": "61%",
6148+
"PassRate": "60%",
61496149
"Difficulty": "Medium",
61506150
"IsAccepted": false,
61516151
"IsPaid": true,
@@ -6217,7 +6217,7 @@
62176217
"ID": 516,
62186218
"Title": "Longest Palindromic Subsequence",
62196219
"TitleSlug": "longest-palindromic-subsequence",
6220-
"PassRate": "44%",
6220+
"PassRate": "45%",
62216221
"Difficulty": "Medium",
62226222
"IsAccepted": true,
62236223
"IsPaid": false,
@@ -6793,7 +6793,7 @@
67936793
"ID": 564,
67946794
"Title": "Find the Closest Palindrome",
67956795
"TitleSlug": "find-the-closest-palindrome",
6796-
"PassRate": "18%",
6796+
"PassRate": "17%",
67976797
"Difficulty": "Hard",
67986798
"IsAccepted": true,
67996799
"IsPaid": false,
@@ -7477,7 +7477,7 @@
74777477
"ID": 621,
74787478
"Title": "Task Scheduler",
74797479
"TitleSlug": "task-scheduler",
7480-
"PassRate": "43%",
7480+
"PassRate": "44%",
74817481
"Difficulty": "Medium",
74827482
"IsAccepted": true,
74837483
"IsPaid": false,
@@ -7825,7 +7825,7 @@
78257825
"ID": 650,
78267826
"Title": "2 Keys Keyboard",
78277827
"TitleSlug": "2-keys-keyboard",
7828-
"PassRate": "45%",
7828+
"PassRate": "46%",
78297829
"Difficulty": "Medium",
78307830
"IsAccepted": true,
78317831
"IsPaid": false,
@@ -7969,7 +7969,7 @@
79697969
"ID": 662,
79707970
"Title": "Maximum Width of Binary Tree",
79717971
"TitleSlug": "maximum-width-of-binary-tree",
7972-
"PassRate": "40%",
7972+
"PassRate": "39%",
79737973
"Difficulty": "Medium",
79747974
"IsAccepted": true,
79757975
"IsPaid": false,
@@ -8101,7 +8101,7 @@
81018101
"ID": 673,
81028102
"Title": "Number of Longest Increasing Subsequence",
81038103
"TitleSlug": "number-of-longest-increasing-subsequence",
8104-
"PassRate": "32%",
8104+
"PassRate": "33%",
81058105
"Difficulty": "Medium",
81068106
"IsAccepted": true,
81078107
"IsPaid": false,
@@ -8605,7 +8605,7 @@
86058605
"ID": 715,
86068606
"Title": "Range Module",
86078607
"TitleSlug": "range-module",
8608-
"PassRate": "33%",
8608+
"PassRate": "34%",
86098609
"Difficulty": "Hard",
86108610
"IsAccepted": true,
86118611
"IsPaid": false,
@@ -9949,7 +9949,7 @@
99499949
"ID": 827,
99509950
"Title": "Making A Large Island",
99519951
"TitleSlug": "making-a-large-island",
9952-
"PassRate": "41%",
9952+
"PassRate": "42%",
99539953
"Difficulty": "Hard",
99549954
"IsAccepted": true,
99559955
"IsPaid": false,
@@ -10297,7 +10297,7 @@
1029710297
"ID": 856,
1029810298
"Title": "Score of Parentheses",
1029910299
"TitleSlug": "score-of-parentheses",
10300-
"PassRate": "55%",
10300+
"PassRate": "54%",
1030110301
"Difficulty": "Medium",
1030210302
"IsAccepted": true,
1030310303
"IsPaid": false,
@@ -10765,7 +10765,7 @@
1076510765
"ID": 895,
1076610766
"Title": "Maximum Frequency Stack",
1076710767
"TitleSlug": "maximum-frequency-stack",
10768-
"PassRate": "50%",
10768+
"PassRate": "51%",
1076910769
"Difficulty": "Hard",
1077010770
"IsAccepted": true,
1077110771
"IsPaid": false,
@@ -11389,7 +11389,7 @@
1138911389
"ID": 947,
1139011390
"Title": "Most Stones Removed with Same Row or Column",
1139111391
"TitleSlug": "most-stones-removed-with-same-row-or-column",
11392-
"PassRate": "52%",
11392+
"PassRate": "53%",
1139311393
"Difficulty": "Medium",
1139411394
"IsAccepted": false,
1139511395
"IsPaid": false,
@@ -11437,7 +11437,7 @@
1143711437
"ID": 951,
1143811438
"Title": "Flip Equivalent Binary Trees",
1143911439
"TitleSlug": "flip-equivalent-binary-trees",
11440-
"PassRate": "64%",
11440+
"PassRate": "65%",
1144111441
"Difficulty": "Medium",
1144211442
"IsAccepted": false,
1144311443
"IsPaid": false,
@@ -11701,7 +11701,7 @@
1170111701
"ID": 973,
1170211702
"Title": "K Closest Points to Origin",
1170311703
"TitleSlug": "k-closest-points-to-origin",
11704-
"PassRate": "67%",
11704+
"PassRate": "66%",
1170511705
"Difficulty": "Easy",
1170611706
"IsAccepted": false,
1170711707
"IsPaid": false,
@@ -11857,7 +11857,7 @@
1185711857
"ID": 986,
1185811858
"Title": "Interval List Intersections",
1185911859
"TitleSlug": "interval-list-intersections",
11860-
"PassRate": "57%",
11860+
"PassRate": "58%",
1186111861
"Difficulty": "Medium",
1186211862
"IsAccepted": false,
1186311863
"IsPaid": false,
@@ -11869,7 +11869,7 @@
1186911869
"ID": 987,
1187011870
"Title": "Vertical Order Traversal of a Binary Tree",
1187111871
"TitleSlug": "vertical-order-traversal-of-a-binary-tree",
11872-
"PassRate": "33%",
11872+
"PassRate": "32%",
1187311873
"Difficulty": "Medium",
1187411874
"IsAccepted": false,
1187511875
"IsPaid": false,
@@ -11881,7 +11881,7 @@
1188111881
"ID": 988,
1188211882
"Title": "Smallest String Starting From Leaf",
1188311883
"TitleSlug": "smallest-string-starting-from-leaf",
11884-
"PassRate": "52%",
11884+
"PassRate": "51%",
1188511885
"Difficulty": "Medium",
1188611886
"IsAccepted": false,
1188711887
"IsPaid": false,

0 commit comments

Comments
 (0)