{"id":2422,"date":"2018-04-05T21:08:37","date_gmt":"2018-04-06T04:08:37","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2422"},"modified":"2018-04-05T21:31:47","modified_gmt":"2018-04-06T04:31:47","slug":"leetcode-453-minimum-moves-to-equal-array-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-453-minimum-moves-to-equal-array-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 453. Minimum Moves to Equal Array Elements"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u6bcf\u6b21\u53ef\u4ee5\u628a\u5176\u4e2dn-1\u4e2a\u6570\u52a01\uff0c\u95ee\u6700\u5c11\u9700\u8981\u591a\u5c11\u6b21\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u5f97\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u90fd\u76f8\u7b49\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/minimum-moves-to-equal-array-elements\/description\/\">https:\/\/leetcode.com\/problems\/minimum-moves-to-equal-array-elements\/description\/<\/a><\/p>\n<p>Given a\u00a0<b>non-empty<\/b>\u00a0integer array of size\u00a0<i>n<\/i>, find the minimum number of moves required to make all array elements equal, where a move is incrementing\u00a0<i>n<\/i>\u00a0&#8211; 1 elements by 1.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>\r\n[1,2,3]\r\n\r\n<b>Output:<\/b>\r\n3\r\n\r\n<b>Explanation:<\/b>\r\nOnly three moves are needed (remember each move increments two elements):\r\n\r\n[1,2,3]  =&gt;  [2,3,3]  =&gt;  [3,4,3]  =&gt;  [4,4,4]<\/pre>\n<h1><strong>Idea<\/strong><\/h1>\n<p>Assuming the sum of array is <strong>S<\/strong>, the minimum element of the array is <strong>min<\/strong> and minimum number of moves is <strong>m<\/strong>.<\/p>\n<p>Each move will increase the sum of array by n &#8211; 1. Finally, every element becomes <strong>x<\/strong>. So we have:<\/p>\n<ol>\n<li>S + (n &#8211; 1) * m = x * n<\/li>\n<li>min + m = x<\/li>\n<\/ol>\n<p>We got: m = S &#8211; n * min<\/p>\n<h1><strong>Solution: Math<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 51 ms\r\nclass Solution {\r\npublic:\r\n  int minMoves(const vector&lt;int&gt;&amp; a) {\r\n    return accumulate(a.begin(), a.end(), 0L) - a.size() * *min_element(a.begin(), a.end());\r\n  }\r\n};<\/pre>\n<p>Python<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 104 ms\r\n\"\"\"\r\nclass Solution:\r\n  def minMoves(self, nums):\r\n    return sum(nums) - len(nums) * min(nums)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u6bcf\u6b21\u53ef\u4ee5\u628a\u5176\u4e2dn-1\u4e2a\u6570\u52a01\uff0c\u95ee\u6700\u5c11\u9700\u8981\u591a\u5c11\u6b21\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u5f97\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u90fd\u76f8\u7b49\u3002 https:\/\/leetcode.com\/problems\/minimum-moves-to-equal-array-elements\/description\/ Given a\u00a0non-empty\u00a0integer array of size\u00a0n, find the minimum number of moves required to make all array elements equal, where a move is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,31],"class_list":["post-2422","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2422","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=2422"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2422\/revisions"}],"predecessor-version":[{"id":2429,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2422\/revisions\/2429"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}