{"id":524,"date":"2017-10-02T23:43:40","date_gmt":"2017-10-03T06:43:40","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=524"},"modified":"2018-08-23T23:47:55","modified_gmt":"2018-08-24T06:47:55","slug":"leetcode-128-longest-consecutive-sequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-128-longest-consecutive-sequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 128. Longest Consecutive Sequence"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 128. Longest Consecutive Sequence - \u5237\u9898\u627e\u5de5\u4f5c EP80\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/rc2QdQ7U78I?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<p>&nbsp;<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given an unsorted array of integers, find the length of the longest consecutive elements sequence.<\/p>\n<p>For example,<br \/>\nGiven\u00a0<code>[100, 4, 200, 1, 3, 2]<\/code>,<br \/>\nThe longest consecutive elements sequence is\u00a0<code>[1, 2, 3, 4]<\/code>. Return its length:\u00a0<code>4<\/code>.<\/p>\n<p>Your algorithm should run in O(<i>n<\/i>) complexity.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Hashtable \/ Hashset<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-528\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-527\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/128-ep80-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Time complexity: O(n)<\/strong><\/p>\n<p><strong>Space complexity: O(n)<\/strong><\/p>\n<p><strong>Solution 1: <\/strong>C++ \/ online<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n    int longestConsecutive(vector&lt;int&gt;&amp; nums) {\r\n        unordered_map&lt;int, int&gt; h;\r\n        int ans = 0;\r\n        for (int num : nums) {\r\n            if (h.count(num)) continue;\r\n            \r\n            auto it_l = h.find(num - 1);\r\n            auto it_r = h.find(num + 1);\r\n            \r\n            int l = it_l != h.end() ? it_l-&gt;second : 0;\r\n            int r = it_r != h.end() ? it_r-&gt;second : 0;\r\n            int t = l + r + 1;\r\n            \r\n            h[num] = h[num - l] = h[num + r] = t;\r\n            \r\n            ans = max(ans, t);            \r\n        }\r\n        \r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Solution 2: C++ \/ offline<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n    int longestConsecutive(vector&lt;int&gt;&amp; nums) {\r\n        unordered_set&lt;int&gt; h(nums.begin(), nums.end());\r\n        int ans = 0;\r\n        for (int num : nums)            \r\n            if (!h.count(num - 1)) {\r\n                int l = 0;\r\n                while (h.count(num++)) ++l;\r\n                ans = max(ans, l);\r\n            }\r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Problem: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given\u00a0[100, 4, 200, 1, 3, 2],&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[217,82,117,100],"class_list":["post-524","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hard","tag-hashtable","tag-longest","tag-sequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/524","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=524"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/524\/revisions"}],"predecessor-version":[{"id":3682,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/524\/revisions\/3682"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}