{"id":2493,"date":"2018-04-14T20:34:29","date_gmt":"2018-04-15T03:34:29","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2493"},"modified":"2018-04-15T10:04:35","modified_gmt":"2018-04-15T17:04:35","slug":"leetcode-817-linked-list-components","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-817-linked-list-components\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 817. Linked List Components"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 817. Linked List Components - \u5237\u9898\u627e\u5de5\u4f5c EP181\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/y1_lFSkQNDc?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>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u518d\u7ed9\u4f60\u4e00\u4e9b\u5408\u6cd5\u7684\u8282\u70b9\uff0c\u95ee\u4f60\u94fe\u8868\u4e2d\u6709\u591a\u5c11\u4e2a\u8fde\u901a\u5206\u91cf\uff08\u6240\u6709\u8282\u70b9\u5fc5\u987b\u5408\u6cd5\uff09\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/linked-list-components\/description\/\">https:\/\/leetcode.com\/problems\/linked-list-components\/description\/<\/a><\/p>\n<p>We are given\u00a0<code>head<\/code>,\u00a0the head node of a linked list containing\u00a0<strong>unique integer values<\/strong>.<\/p>\n<p>We are also given the list\u00a0<code>G<\/code>, a subset of the values in the linked list.<\/p>\n<p>Return the number of connected components in\u00a0<code>G<\/code>, where two values are connected if they appear consecutively in the linked list.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \r\nhead: 0-&gt;1-&gt;2-&gt;3\r\nG = [0, 1, 3]\r\n<strong>Output:<\/strong> 2\r\n<strong>Explanation:<\/strong> \r\n0 and 1 are connected, so [0, 1] and [3] are the two connected components.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \r\nhead: 0-&gt;1-&gt;2-&gt;3-&gt;4\r\nG = [0, 3, 1, 4]\r\n<strong>Output:<\/strong> 2\r\n<strong>Explanation:<\/strong> \r\n0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>If\u00a0<code>N<\/code>\u00a0is the\u00a0length of the linked list given by\u00a0<code>head<\/code>,\u00a0<code>1 &lt;= N &lt;= 10000<\/code>.<\/li>\n<li>The value of each node in the linked list will be in the range<code>\u00a0[0, N - 1]<\/code>.<\/li>\n<li><code>1 &lt;= G.length &lt;= 10000<\/code>.<\/li>\n<li><code>G<\/code>\u00a0is a subset of all values in the linked list.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2500\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/817-ep181.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/817-ep181.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/817-ep181-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/817-ep181-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1>Solution1: Graph Traversal using DFS<\/h1>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 76 ms\r\nclass Solution {\r\npublic:\r\n  int numComponents(ListNode* head, vector&lt;int&gt;&amp; G) {\r\n    unordered_set&lt;int&gt; f(G.begin(), G.end());\r\n    unordered_map&lt;int, vector&lt;int&gt;&gt; g;\r\n    int u = head-&gt;val;\r\n    while (head-&gt;next) {\r\n      head = head-&gt;next;\r\n      int v = head-&gt;val;\r\n      if (f.count(v) &amp;&amp; f.count(u)) {\r\n        g[u].push_back(v);\r\n        g[v].push_back(u);\r\n      }\r\n      u = v;\r\n    }\r\n    int ans = 0;\r\n    unordered_set&lt;int&gt; visited;\r\n    for (int u : G) {      \r\n      if (visited.count(u)) continue;\r\n      ++ans;\r\n      dfs(u, g, visited);\r\n    }\r\n    return ans;\r\n  }\r\nprivate:\r\n  void dfs(int cur, unordered_map&lt;int, vector&lt;int&gt;&gt;&amp; g, unordered_set&lt;int&gt;&amp; visited) {\r\n    if (visited.count(cur)) return;\r\n    visited.insert(cur);\r\n    for (const int next : g[cur])\r\n      dfs(next, g, visited);    \r\n  }\r\n};<\/pre>\n<h1>Solution 2: Count tail node in sub graph<\/h1>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 35 ms\r\nclass Solution {\r\npublic:\r\n  int numComponents(ListNode* head, vector&lt;int&gt;&amp; G) {\r\n    unordered_set&lt;int&gt; g(G.begin(), G.end());\r\n    int ans = 0;\r\n    while (head) {\r\n      if (g.count(head-&gt;val) &amp;&amp; (!head-&gt;next || !g.count(head-&gt;next-&gt;val)))\r\n        ++ans;\r\n      head = head-&gt;next;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u518d\u7ed9\u4f60\u4e00\u4e9b\u5408\u6cd5\u7684\u8282\u70b9\uff0c\u95ee\u4f60\u94fe\u8868\u4e2d\u6709\u591a\u5c11\u4e2a\u8fde\u901a\u5206\u91cf\uff08\u6240\u6709\u8282\u70b9\u5fc5\u987b\u5408\u6cd5\uff09\u3002 https:\/\/leetcode.com\/problems\/linked-list-components\/description\/ We are given\u00a0head,\u00a0the head node of a linked list containing\u00a0unique integer values. We are also given the list\u00a0G, a subset of the&#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,50],"tags":[102,77,83,177],"class_list":["post-2493","post","type-post","status-publish","format-standard","hentry","category-graph","category-list","tag-connected-components","tag-graph","tag-list","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2493","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=2493"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2493\/revisions"}],"predecessor-version":[{"id":2501,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2493\/revisions\/2501"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}