{"id":2365,"date":"2018-03-24T19:46:20","date_gmt":"2018-03-25T02:46:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2365"},"modified":"2018-03-25T01:34:19","modified_gmt":"2018-03-25T08:34:19","slug":"leetcode-805-split-array-with-same-average","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-805-split-array-with-same-average\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 805. Split Array With Same Average"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u95ee\u80fd\u5426\u5c06\u4e00\u4e2a\u6570\u7ec4\u5206\u6210\u4e24\u90e8\u5206\uff0c\u6bcf\u90e8\u5206\u7684\u5e73\u5747\u503c\u76f8\u540c\u3002<\/p>\n<p>In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.)<\/p>\n<p>Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty.<\/p>\n<pre class=\"crayon:false \"><strong>Example :<\/strong>\r\n<strong>Input:<\/strong> \r\n[1,2,3,4,5,6,7,8]\r\n<strong>Output:<\/strong> true\r\n<strong>Explanation: <\/strong>We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have the average of 4.5.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The length of\u00a0<code>A<\/code>\u00a0will be in the range\u00a0[1, 30].<\/li>\n<li><code>A[i]<\/code>\u00a0will be in the range of\u00a0<code>[0, 10000]<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: Search<\/strong><\/h1>\n<p>Time complexity: O(2^n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 771 ms\r\nclass Solution {\r\npublic:\r\n  bool splitArraySameAverage(vector&lt;int&gt;&amp; A) {\r\n    std::sort(A.begin(), A.end());\r\n    sum = std::accumulate(A.begin(), A.end(), 0);\r\n    n = A.size();\r\n    return dfs(A, 1, 0, 0);\r\n  }\r\nprivate:\r\n  int sum;\r\n  int n;\r\n  bool dfs(const vector&lt;int&gt;&amp; A, int c, int s, int cur) {\r\n    if (c &gt; A.size() \/ 2) return false;\r\n    for (int i = s; i &lt; A.size(); ++i) {\r\n      cur += A[i];      \r\n      if (cur * (n - c)  == (sum - cur) * c) return true;\r\n      if (cur * (n - c)  &gt; (sum - cur) * c) break;\r\n      if (dfs(A, c + 1, i + 1, cur)) return true;\r\n      cur -= A[i];\r\n    }\r\n    return false;\r\n  } \r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u95ee\u80fd\u5426\u5c06\u4e00\u4e2a\u6570\u7ec4\u5206\u6210\u4e24\u90e8\u5206\uff0c\u6bcf\u90e8\u5206\u7684\u5e73\u5747\u503c\u76f8\u540c\u3002 In a given integer array A, we must move every element of A to either list B or list C. (B and C&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[277,33,217,42,276,62],"class_list":["post-2365","post","type-post","status-publish","format-standard","hentry","category-searching","tag-average","tag-dfs","tag-hard","tag-search","tag-split","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2365","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=2365"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2365\/revisions"}],"predecessor-version":[{"id":2389,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2365\/revisions\/2389"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}