[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph:

  • There are A.length nodes, labelled A[0] to A[A.length - 1];
  • 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.

Return the size of the largest connected component in the graph.

Example 1:

Input: [4,6,15,35]
Output: 4

Example 2:

Input: [20,50,9,63]
Output: 2

Example 3:

Input: [2,3,6,7,4,12,21,39]
Output: 8

Note:

  1. 1 <= A.length <= 20000
  2. 1 <= A[i] <= 100000


给定一个由不同正整数的组成的非空数组 A,考虑下面的图:

  • 有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;
  • 只有当 A[i] 和 A[j] 共用一个大于 1 的公因数时,A[i] 和 A[j] 之间才有一条边。

返回图中最大连通组件的大小。

示例 1:

输入:[4,6,15,35]
输出:4

示例 2:

输入:[20,50,9,63]
输出:2

示例 3:

输入:[2,3,6,7,4,12,21,39]
输出:8

提示:

  1. 1 <= A.length <= 20000
  2. 1 <= A[i] <= 100000

3332 ms

  1 class Solution {
  2     func largestComponentSize(_ A: [Int]) -> Int {
  3         var lpf:[Int] = enumLowestPrimeFactors(100001)
  4         var ds:DJSet = DJSet(100001)
  5         for v in A
  6         {
  7             ds.w[v] = 1
  8         }
  9         for v in A
 10         {
 11             var vv:Int = v
 12             while(vv > 1)
 13             {
 14                 ds.union(v, lpf[vv])
 15                 vv /= lpf[vv]
 16             }
 17         }
 18         var ret:Int = 0
 19         for i in 1...100000
 20         {
 21             if ds.upper[i] < 0
 22             {
 23                 ret = max(ret, ds.w[i])
 24             }
 25         }
 26         return ret
 27     }
 28
 29     func enumLowestPrimeFactors(_ n:Int) -> [Int]
 30     {
 31         var tot:Int = 0
 32         var lpf:[Int] = [Int](repeating:0,count:n + 1)
 33         var u:Double = Double(n + 32)
 34         var lu:Double = log(u)
 35         let num:Int = Int(u / lu + u / lu / lu * 1.5)
 36         var primes:[Int] = [Int](repeating:0,count:num)
 37         for i in 2...n
 38         {
 39             lpf[i] = i
 40         }
 41         for p in 2...n
 42         {
 43             if lpf[p] == p
 44             {
 45                 primes[tot] = p
 46                 tot += 1
 47             }
 48             var tmp:Int = 0
 49             var i:Int = 0
 50             while(i < tot && primes[i] <= lpf[p] && primes[i] * p <= n)
 51             {
 52                 tmp = primes[i] * p
 53                 lpf[tmp] = primes[i]
 54                 i += 1
 55             }
 56         }
 57         return lpf
 58     }
 59 }
 60 public class DJSet
 61 {
 62     var upper:[Int]
 63     var w:[Int]
 64
 65     init(_ n:Int)
 66     {
 67         upper = [Int](repeating:-1,count:n)
 68         w = [Int](repeating:0,count:n)
 69     }
 70
 71     func root(_ x:Int) -> Int
 72     {
 73         if(upper[x] < 0)
 74         {
 75             return x
 76         }
 77         else
 78         {
 79             upper[x] = root(upper[x])
 80             return upper[x]
 81         }
 82     }
 83
 84     func equiv(_ x:Int,_ y:Int) -> Bool
 85     {
 86         return root(x) == root(y)
 87     }
 88
 89     func union(_ x:Int,_ y:Int) -> Bool
 90     {
 91         var x:Int = root(x)
 92         var y:Int = root(y)
 93         if x != y
 94         {
 95             if upper[y] < upper[x]
 96             {
 97                 var d:Int = x
 98                 x = y
 99                 y = d
100             }
101             upper[x] += upper[y]
102             upper[y] = x
103             w[x] += w[y]
104         }
105         return x == y
106     }
107
108     func count() -> Int
109     {
110         var ct:Int = 0
111         for u in upper
112         {
113             if u < 0
114             {
115                 ct += 1
116             }
117         }
118         return ct
119     }
120 }

原文地址:https://www.cnblogs.com/strengthen/p/10053034.html

时间: 2024-11-29 10:19:48

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor的相关文章

[Swift Weekly Contest 113]LeetCode950. 按递增顺序显示卡牌 | Reveal Cards In Increasing Order

In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. Now, you do the following steps repeatedly, until all cards are revealed: Take the

[Swift Weekly Contest 116]LeetCode961. 重复 N 次的元素 | N-Repeated Element in Size 2N Array

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input: [5,1

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 109]LeetCode936. 戳印序列 | Stamping The Sequence

You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the s