{"id":4211,"date":"2018-10-20T22:46:41","date_gmt":"2018-10-21T05:46:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4211"},"modified":"2018-10-20T22:47:25","modified_gmt":"2018-10-21T05:47:25","slug":"leetcode-927-three-equal-parts","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-927-three-equal-parts\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 927. Three Equal Parts"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>A<\/code>\u00a0of\u00a0<code>0<\/code>s and\u00a0<code>1<\/code>s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value.<\/p>\n<p>If it is possible, return\u00a0<strong>any<\/strong>\u00a0<code>[i, j]<\/code>\u00a0with\u00a0<code>i+1 &lt; j<\/code>, such that:<\/p>\n<ul>\n<li><code>A[0], A[1], ..., A[i]<\/code>\u00a0is the first part;<\/li>\n<li><code>A[i+1], A[i+2], ..., A[j-1]<\/code>\u00a0is the second part, and<\/li>\n<li><code>A[j], A[j+1], ..., A[A.length - 1]<\/code>\u00a0is the third part.<\/li>\n<li>All three parts have equal binary value.<\/li>\n<\/ul>\n<p>If it is not possible, return\u00a0<code>[-1, -1]<\/code>.<\/p>\n<p>Note that the entire part is used when considering what binary value it represents.\u00a0 For example,\u00a0<code>[1,1,0]<\/code>\u00a0represents\u00a0<code>6<\/code>\u00a0in decimal,\u00a0not\u00a0<code>3<\/code>.\u00a0 Also, leading zeros are allowed, so\u00a0<code>[0,1,1]<\/code>\u00a0and\u00a0<code>[1,1]<\/code>\u00a0represent the same value.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[1,0,1,0,1]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[0,3]<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[1,1,0,1,1]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[-1,-1]<\/span><\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>3 &lt;= A.length &lt;= 30000<\/code><\/li>\n<li><code>A[i] == 0<\/code>\u00a0or\u00a0<code>A[i] == 1<\/code><\/li>\n<\/ol>\n<h1><strong>Solution:<\/strong><\/h1>\n<p>each part should have the same number of 1 s.<\/p>\n<p>Find the suffix (without leading os) of the last part which should have 1\/3 of the total ones.<\/p>\n<p>Time complexity: O(n^2) in theory but close to O(n) in practice<\/p>\n<p>Space complexity: O(n)<\/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, 44 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; threeEqualParts(vector&lt;int&gt;&amp; A) {    \r\n    string s(begin(A), end(A));\r\n    int ones = accumulate(begin(A), end(A), 0);\r\n    if (ones % 3 != 0) return {-1, -1};\r\n    if (ones == 0) return {0, A.size() - 1};\r\n    ones \/= 3;\r\n    int right = A.size() - 1;\r\n    while (ones) if (A[right--]) --ones;\r\n    string suffix(begin(s) + right + 1, end(s));\r\n    size_t l = suffix.length();\r\n    size_t left = s.find(suffix);\r\n    if (left == std::string::npos) return {-1, -1};\r\n    size_t mid = s.find(suffix, left + l);\r\n    if (mid == std::string::npos \r\n       || mid + 2 * l &gt; s.length()) return {-1, -1};\r\n    return {left + l - 1, mid + l};\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0A\u00a0of\u00a00s and\u00a01s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value. If it&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[16,217,424,276],"class_list":["post-4211","post","type-post","status-publish","format-standard","hentry","category-string","tag-bit","tag-hard","tag-pattern","tag-split","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4211","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=4211"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4211\/revisions"}],"predecessor-version":[{"id":4213,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4211\/revisions\/4213"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}