{"id":2966,"date":"2018-06-30T12:18:08","date_gmt":"2018-06-30T19:18:08","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2966"},"modified":"2018-07-01T10:56:52","modified_gmt":"2018-07-01T17:56:52","slug":"leetcode-525-contiguous-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-525-contiguous-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 525. Contiguous Array"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 525. Contiguous Array - \u5237\u9898\u627e\u5de5\u4f5c EP201\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/uAGt1QoAoMU?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>Problem<\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42\u6700\u957f\u5b50\u6570\u7ec4\uff0c\u8981\u6c42\u5176\u4e2d0\u548c1\u7684\u6570\u91cf\u76f8\u7b49\u3002<\/p>\n<p>Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [0,1]\r\n<b>Output:<\/b> 2\r\n<b>Explanation:<\/b> [0, 1] is the longest contiguous subarray with equal number of 0 and 1.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [0,1,0]\r\n<b>Output:<\/b> 2\r\n<b>Explanation:<\/b> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0The length of the given binary array will not exceed 50,000.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2979\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/525-ep201.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/525-ep201.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/525-ep201-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/525-ep201-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1>Solution: HashTable<\/h1>\n<p>Prefix sum + hashtable<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 143 ms\r\nclass Solution {\r\npublic:\r\n  int findMaxLength(vector&lt;int&gt;&amp; nums) {\r\n    if (nums.empty()) return 0;\r\n    unordered_map&lt;int, int&gt; pos;\r\n    int sum = 0;\r\n    int ans = 0;    \r\n    for (int i = 0; i &lt; nums.size(); ++i) {\r\n      sum += nums[i] ? 1 : -1;\r\n      if (sum == 0) {\r\n        ans = i + 1;  \r\n      } else if (pos.count(sum)) {\r\n        ans = max(ans, i - pos[sum]);        \r\n      } else {\r\n        pos[sum] = i;\r\n      } \r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>V2: Using array instead of a hashtable<\/p>\n<p>Time complexity: O(2*n + 1 + n)<\/p>\n<p>Space complexity: O(2*n + 1)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 102 ms (&lt;96.8 %)\r\nclass Solution {\r\npublic:\r\n  int findMaxLength(vector&lt;int&gt;&amp; nums) {    \r\n    if (nums.empty()) return 0;\r\n    const int n = nums.size();\r\n    vector&lt;int&gt; pos(2 * n + 1, INT_MIN);\r\n    int sum = 0;\r\n    int ans = 0;    \r\n    for (int i = 0; i &lt; nums.size(); ++i) {\r\n      sum += nums[i] ? 1 : -1;\r\n      if (sum == 0) {\r\n        ans = i + 1;  \r\n      } else if (pos[sum + n] != INT_MIN) {\r\n        ans = max(ans, i - pos[sum + n]);\r\n      } else {\r\n        pos[sum + n] = i;\r\n      } \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\u6c42\u6700\u957f\u5b50\u6570\u7ec4\uff0c\u8981\u6c42\u5176\u4e2d0\u548c1\u7684\u6570\u91cf\u76f8\u7b49\u3002 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1]&#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":[82,177,200],"class_list":["post-2966","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2966","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=2966"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2966\/revisions"}],"predecessor-version":[{"id":2980,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2966\/revisions\/2980"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}