{"id":4392,"date":"2018-12-01T23:14:51","date_gmt":"2018-12-02T07:14:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4392"},"modified":"2018-12-02T19:10:34","modified_gmt":"2018-12-03T03:10:34","slug":"leetcode-952-largest-component-size-by-common-factor","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-952-largest-component-size-by-common-factor\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 952. Largest Component Size by Common Factor"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 952. Largest Component Size by Common Factor - \u5237\u9898\u627e\u5de5\u4f5c EP232\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/GTX0kw63Tn0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given a non-empty\u00a0array of unique positive integers\u00a0<code>A<\/code>, consider the following graph:<\/p>\n<ul>\n<li>There are\u00a0<code>A.length<\/code>\u00a0nodes, labelled\u00a0<code>A[0]<\/code>\u00a0to\u00a0<code>A[A.length - 1];<\/code><\/li>\n<li>There is an edge between\u00a0<code>A[i]<\/code>\u00a0and\u00a0<code>A[j]<\/code>\u00a0if and only if\u00a0<code>A[i]<\/code>\u00a0and\u00a0<code>A[j]<\/code>\u00a0share a common factor greater than 1.<\/li>\n<\/ul>\n<p>Return the size of the largest connected component in the graph.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[4,6,15,35]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">4<\/span>\r\n<img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/01\/ex1.png\" alt=\"\" \/>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[20,50,9,63]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">2<\/span>\r\n<img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/01\/ex2.png\" alt=\"\" \/>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">[2,3,6,7,4,12,21,39]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">8<\/span>\r\n<img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/01\/ex3.png\" alt=\"\" \/>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length &lt;= 20000<\/code><\/li>\n<li><code>1 &lt;= A[i] &lt;= 100000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Union Find<\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4400\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/952-ep232.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/952-ep232.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/952-ep232-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/952-ep232-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>For each number, union itself with all its factors.<\/p>\n<p>E.g. 6, union(6,2), union(6,3)<\/p>\n<p>Time complexity:\u00a0\\( O(\\Sigma{sqrt(A[i])})\u00a0\u00a0\\)<\/p>\n<p>Space complexity: \\( O(max(A)) \\)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, running time: 148 ms\r\nclass DSU {\r\npublic:\r\n  DSU(int n): p_(n) {\r\n    for (int i = 0; i &lt; n; ++i)\r\n      p_[i] = i;\r\n  }\r\n  \r\n  void Union(int x, int y) {\r\n    p_[Find(x)] = p_[Find(y)];\r\n  }\r\n  \r\n  int Find(int x) {\r\n    if (p_[x] != x) p_[x] = Find(p_[x]);\r\n    return p_[x];\r\n  }\r\nprivate:\r\n  vector&lt;int&gt; p_;\r\n};\r\n\r\nclass Solution {\r\npublic:\r\n  int largestComponentSize(vector&lt;int&gt;&amp; A) {    \r\n    int n = *max_element(begin(A), end(A));\r\n    DSU dsu(n + 1);\r\n    for (int a : A) {\r\n      int t = sqrt(a);\r\n      for (int k = 2; k &lt;= t; ++k)\r\n        if (a % k == 0) {\r\n          dsu.Union(a, k);\r\n          dsu.Union(a, a \/ k);\r\n        }\r\n    }\r\n    unordered_map&lt;int, int&gt; c;\r\n    int ans = 1;\r\n    for (int a : A)\r\n      ans = max(ans, ++c[dsu.Find(a)]);    \r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua, running time: 3432 ms\r\nclass Solution:\r\n  def largestComponentSize(self, A):\r\n    p = list(range(max(A) + 1))\r\n       \r\n    def find(x):\r\n      while p[x] != x:\r\n        p[x] = p[p[x]]\r\n        x = p[x]\r\n      return x\r\n    \r\n    def union(x, y):\r\n      p[find(x)] = p[find(y)]      \r\n      \r\n    for a in A:     \r\n      for k in range(2, int(math.sqrt(a) + 1)):        \r\n        if a % k == 0:\r\n          union(a, k)\r\n          union(a, a \/\/ k)\r\n    \r\n    return collections.Counter([find(a) for a in A]).most_common(1)[0][1]<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a non-empty\u00a0array of unique positive integers\u00a0A, consider the following graph: There are\u00a0A.length\u00a0nodes, labelled\u00a0A[0]\u00a0to\u00a0A[A.length &#8211; 1]; There is an edge between\u00a0A[i]\u00a0and\u00a0A[j]\u00a0if and only if\u00a0A[i]\u00a0and\u00a0A[j]\u00a0share&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[441,77,217,113],"class_list":["post-4392","post","type-post","status-publish","format-standard","hentry","category-graph","tag-factor","tag-graph","tag-hard","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4392"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4392\/revisions"}],"predecessor-version":[{"id":4404,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4392\/revisions\/4404"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}