{"id":2548,"date":"2018-05-02T19:59:56","date_gmt":"2018-05-02T11:59:56","guid":{"rendered":"https:\/\/boweihe.me\/?p=2548"},"modified":"2018-05-02T19:59:56","modified_gmt":"2018-05-02T11:59:56","slug":"leetcode-128-longest-consecutive-sequence","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2548","title":{"rendered":"LeetCode 128. Longest Consecutive Sequence"},"content":{"rendered":"<p>https:\/\/leetcode.com\/problems\/longest-consecutive-sequence\/description\/<\/p>\n<pre class=\"theme:github lang:c++ decode:true  \">class Solution {\npublic:\n    int longestConsecutive(vector&lt;int&gt;&amp; nums) {\n        unordered_set&lt;int&gt; all_nums;\n        unordered_map&lt;int, int&gt; unions_rev; \/\/ &lt;end_idx, start_idx&gt;\n        unordered_map&lt;int, int&gt; unions;     \/\/ &lt;start_idx, end_idx&gt;\n        int max_length = 0;\n        for (int num : nums)\n        {\n            auto it_r_union = unions_rev.find(num - 1);\n            auto it_union = unions.find(num + 1);\n            int start_idx = num;\n            int end_idx = num;\n            if (all_nums.find(num) != all_nums.end())\n                continue;\n            if (it_union != unions.end() &amp;&amp; it_r_union != unions_rev.end())\n            {\n                \/\/ [RRR] num [SSS]\n                start_idx = it_r_union-&gt;second;\n                end_idx = it_union-&gt;second;\n                unions_rev.erase(it_r_union);\n                unions.erase(start_idx);\n                unions.erase(it_union);\n                unions_rev.erase(end_idx);\n            }\n            else if (it_union != unions.end())\n            {\n                \/\/ num [SSS]\n                end_idx = it_union-&gt;second;\n                unions.erase(it_union);\n                unions_rev.erase(end_idx);\n            }\n            else if (it_r_union != unions_rev.end())\n            {\n                \/\/ [RRR] num\n                start_idx = it_r_union-&gt;second;\n                unions_rev.erase(it_r_union);\n                unions.erase(start_idx);\n            }\n            unions[start_idx] = end_idx;\n            unions_rev[end_idx] = start_idx;\n            if (end_idx - start_idx + 1 &gt; max_length)\n            {\n                max_length = end_idx - start_idx + 1;\n            }\n            all_nums.insert(num);\n        }\n        return max_length;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode.com\/problems\/longest-consecutive-sequence\/description\/ class Solution { public: int longestConsecutive(vector&lt;int&gt;&amp; nums) { unordered_set&lt;int&gt; all_nums; unordered_map&lt;int, int&gt; unions_rev; \/\/ &lt;end_idx, start_idx&gt; unordered_map&lt;int, int&gt; unions; \/\/ &lt;start_idx, end_idx&gt; int max_length = 0; for (int num : nums) { auto it_r_union = unions_rev.find(num &#8211; 1); auto it_union = unions.find(num + 1); int start_idx = num; int end_idx = num; if (all_nums.find(num) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[66],"class_list":["post-2548","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2548"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2548\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}