{"id":4371,"date":"2018-11-29T19:40:33","date_gmt":"2018-11-30T03:40:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4371"},"modified":"2018-11-29T19:51:26","modified_gmt":"2018-11-30T03:51:26","slug":"leetcode-945-minimum-increment-to-make-array-unique","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-945-minimum-increment-to-make-array-unique\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 945. Minimum Increment to Make Array Unique"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array of integers A, a\u00a0<em>move<\/em>\u00a0consists of choosing any\u00a0<code>A[i]<\/code>, and incrementing it by\u00a0<code>1<\/code>.<\/p>\n<p>Return the least number of moves to make every value in\u00a0<code>A<\/code>\u00a0unique.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[1,2,2]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">1<\/span>\r\n<strong>Explanation: <\/strong> After 1 move, the array could be [1, 2, 3].\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong><span id=\"example-input-2-1\">[3,2,1,2,1,7]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">6<\/span>\r\n<strong>Explanation: <\/strong> After 6 moves, the array could be [3, 4, 1, 2, 5, 7].\r\nIt can be shown with 5 or less moves that it is impossible for the array to have all unique values.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= A.length &lt;= 40000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt; 40000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>Sort the elements, make sure A[i] &gt;= A[i-1] + 1, if not increase A[i] to A[i &#8211; 1] + 1<\/p>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(1)<\/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, 56 ms\r\nclass Solution {\r\npublic:\r\n  int minIncrementForUnique(vector&lt;int&gt;&amp; A) {\r\n    int ans = 0;\r\n    sort(begin(A), end(A));\r\n    for (int i = 1; i &lt; A.size(); ++i) {\r\n      if (A[i] &gt; A[i - 1]) continue;\r\n      ans += (A[i - 1] - A[i]) + 1;\r\n      A[i] = A[i - 1] + 1;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua, Running time: 196 ms\r\nclass Solution(object):\r\n  def minIncrementForUnique(self, A):\r\n    A.sort()\r\n    ans = 0\r\n    for i in range(1, len(A)):\r\n      if A[i] &gt; A[i - 1]: continue\r\n      ans += A[i - 1] - A[i] + 1\r\n      A[i] = A[i - 1] + 1\r\n    return ans<\/pre>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array of integers A, a\u00a0move\u00a0consists of choosing any\u00a0A[i], and incrementing it by\u00a01. Return the least number of moves to make every value&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,23,240],"class_list":["post-4371","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-sort","tag-unique","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4371","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=4371"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4371\/revisions"}],"predecessor-version":[{"id":4375,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4371\/revisions\/4375"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}