{"id":8414,"date":"2021-05-01T15:37:29","date_gmt":"2021-05-01T22:37:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8414"},"modified":"2021-05-01T15:37:50","modified_gmt":"2021-05-01T22:37:50","slug":"leetcode-1846-maximum-element-after-decreasing-and-rearranging","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1846-maximum-element-after-decreasing-and-rearranging\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1846. Maximum Element After Decreasing and Rearranging"},"content":{"rendered":"\n<p>You are given an array of positive integers&nbsp;<code>arr<\/code>. Perform some operations (possibly none) on&nbsp;<code>arr<\/code>&nbsp;so that it satisfies these conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The value of the&nbsp;<strong>first<\/strong>&nbsp;element in&nbsp;<code>arr<\/code>&nbsp;must be&nbsp;<code>1<\/code>.<\/li><li>The absolute difference between any 2 adjacent elements must be&nbsp;<strong>less than or equal to&nbsp;<\/strong><code>1<\/code>. In other words,&nbsp;<code>abs(arr[i] - arr[i - 1]) &lt;= 1<\/code>&nbsp;for each&nbsp;<code>i<\/code>&nbsp;where&nbsp;<code>1 &lt;= i &lt; arr.length<\/code>&nbsp;(<strong>0-indexed<\/strong>).&nbsp;<code>abs(x)<\/code>&nbsp;is the absolute value of&nbsp;<code>x<\/code>.<\/li><\/ul>\n\n\n\n<p>There are 2 types of operations that you can perform any number of times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Decrease<\/strong>&nbsp;the value of any element of&nbsp;<code>arr<\/code>&nbsp;to a&nbsp;<strong>smaller positive integer<\/strong>.<\/li><li><strong>Rearrange<\/strong>&nbsp;the elements of&nbsp;<code>arr<\/code>&nbsp;to be in any order.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;possible value of an element in&nbsp;<\/em><code>arr<\/code><em>&nbsp;after performing the operations to satisfy the conditions<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [2,2,1,2,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \nWe can satisfy the conditions by rearranging <code>arr<\/code> so it becomes <code>[1,2,2,2,1]<\/code>.\nThe largest element in <code>arr<\/code> is 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [100,1,1000]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \nOne possible way to satisfy the conditions is by doing the following:\n1. Rearrange <code>arr<\/code> so it becomes <code>[1,100,1000]<\/code>.\n2. Decrease the value of the second element to 2.\n3. Decrease the value of the third element to 3.\nNow <code>arr = [1,2,3], which <\/code>satisfies the conditions.\nThe largest element in <code>arr is 3.<\/code>\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,2,3,4,5]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The array already satisfies the conditions, and the largest element is 5.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sort<\/strong><\/h2>\n\n\n\n<p>arr[0] = 1,<br>arr[i] = min(arr[i], arr[i &#8211; 1] + 1)<\/p>\n\n\n\n<p>ans = arr[n &#8211; 1]<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumElementAfterDecrementingAndRearranging(vector<int>& arr) {\n    const int n = arr.size();\n    sort(begin(arr), end(arr));\n    arr[0] = 1;\n    for (int i = 1; i < n; ++i)\n      arr[i] = min(arr[i], arr[i - 1] + 1);\n    return arr.back();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of positive integers&nbsp;arr. Perform some operations (possibly none) on&nbsp;arr&nbsp;so that it satisfies these conditions: The value of the&nbsp;first&nbsp;element in&nbsp;arr&nbsp;must be&nbsp;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":[184],"tags":[20,177,23],"class_list":["post-8414","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8414","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=8414"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8414\/revisions"}],"predecessor-version":[{"id":8417,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8414\/revisions\/8417"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}