{"id":8540,"date":"2021-08-08T21:47:50","date_gmt":"2021-08-09T04:47:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8540"},"modified":"2021-08-08T21:58:29","modified_gmt":"2021-08-09T04:58:29","slug":"leetcode-1887-reduction-operations-to-make-the-array-elements-equal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1887-reduction-operations-to-make-the-array-elements-equal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1887. Reduction Operations to Make the Array Elements Equal"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>, your goal is to make all elements in&nbsp;<code>nums<\/code>&nbsp;equal. To complete one operation, follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Find the&nbsp;<strong>largest<\/strong>&nbsp;value in&nbsp;<code>nums<\/code>. Let its index be&nbsp;<code>i<\/code>&nbsp;(<strong>0-indexed<\/strong>) and its value be&nbsp;<code>largest<\/code>. If there are multiple elements with the largest value, pick the smallest&nbsp;<code>i<\/code>.<\/li><li>Find the&nbsp;<strong>next largest<\/strong>&nbsp;value in&nbsp;<code>nums<\/code>&nbsp;<strong>strictly smaller<\/strong>&nbsp;than&nbsp;<code>largest<\/code>. Let its value be&nbsp;<code>nextLargest<\/code>.<\/li><li>Reduce&nbsp;<code>nums[i]<\/code>&nbsp;to&nbsp;<code>nextLargest<\/code>.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>the number of operations to make all elements in&nbsp;<\/em><code>nums<\/code><em>&nbsp;equal<\/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> nums = [5,1,3]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>&nbsp;It takes 3 operations to make all elements in nums equal:\n1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].\n2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].\n3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].\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> nums = [1,1,1]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>&nbsp;All elements in nums are already equal.\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> nums = [1,1,2,2,3]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>&nbsp;It takes 4 operations to make all elements in nums equal:\n1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].\n2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].\n3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].\n4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Input: [5,4,3,2,1]<br>[<span style=\"text-decoration: underline;\">5<\/span>,<strong><span class=\"has-inline-color has-vivid-red-color\">4<\/span><\/strong>,3,2,1] -&gt; [<span style=\"text-decoration: underline;\">4<\/span>,4,3,2,1] 5-&gt;4, 1 op<br>[<span style=\"text-decoration: underline;\">4,4<\/span>,<strong><span class=\"has-inline-color has-vivid-red-color\">3<\/span><\/strong>,2,1] -&gt; [<span style=\"text-decoration: underline;\">3,3<\/span>,3,2,1] 4-&gt;3, 2 ops<br>[<span style=\"text-decoration: underline;\">3,3,3<\/span>,<strong><span class=\"has-inline-color has-vivid-red-color\">2<\/span><\/strong>,1] -&gt; [<span style=\"text-decoration: underline;\">2,2,2<\/span>,2,1] 3-&gt;2, 3 ops<br>[<span style=\"text-decoration: underline;\">2,2,2,2<\/span>,<strong><span class=\"has-inline-color has-vivid-red-color\">1<\/span><\/strong>] -&gt; [<span style=\"text-decoration: underline;\">1,1,1,1<\/span>,1] 2-&gt;1, 4 ops<br>total = 1 + 2 + 3 + 4 = 10<\/p>\n\n\n\n<p>Sort the array in reverse order, if we find a number at index i that is is smaller than the previous number, we need i ops to make all the numbers before it to become itself.<\/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 reductionOperations(vector<int>& nums) {    \n    sort(rbegin(nums), rend(nums));\n    int ans = 0;\n    for (int i = 1; i < nums.size(); ++i)\n      if (nums[i] != nums[i - 1]) ans += i;    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums, your goal is to make all elements in&nbsp;nums&nbsp;equal. To complete one operation, follow these steps: Find the&nbsp;largest&nbsp;value in&nbsp;nums. Let its index&#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":[31],"class_list":["post-8540","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8540","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=8540"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8540\/revisions"}],"predecessor-version":[{"id":8542,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8540\/revisions\/8542"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}