|
| 1 | +# 1887. Reduction Operations to Make the Array Elements Equal |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Sorting. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`. |
| 14 | + |
| 15 | +- Find the **next largest** value in `nums` **strictly smaller** than `largest`. Let its value be `nextLargest`. |
| 16 | + |
| 17 | +- Reduce `nums[i]` to `nextLargest`. |
| 18 | + |
| 19 | + |
| 20 | +Return **the number of operations to make all elements in **`nums`** equal**. |
| 21 | + |
| 22 | + |
| 23 | +Example 1: |
| 24 | + |
| 25 | +``` |
| 26 | +Input: nums = [5,1,3] |
| 27 | +Output: 3 |
| 28 | +Explanation: It takes 3 operations to make all elements in nums equal: |
| 29 | +1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3]. |
| 30 | +2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3]. |
| 31 | +3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1]. |
| 32 | +``` |
| 33 | + |
| 34 | +Example 2: |
| 35 | + |
| 36 | +``` |
| 37 | +Input: nums = [1,1,1] |
| 38 | +Output: 0 |
| 39 | +Explanation: All elements in nums are already equal. |
| 40 | +``` |
| 41 | + |
| 42 | +Example 3: |
| 43 | + |
| 44 | +``` |
| 45 | +Input: nums = [1,1,2,2,3] |
| 46 | +Output: 4 |
| 47 | +Explanation: It takes 4 operations to make all elements in nums equal: |
| 48 | +1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2]. |
| 49 | +2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2]. |
| 50 | +3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2]. |
| 51 | +4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1]. |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +**Constraints:** |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +- `1 <= nums.length <= 5 * 104` |
| 60 | + |
| 61 | +- `1 <= nums[i] <= 5 * 104` |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +## Solution |
| 66 | + |
| 67 | +```javascript |
| 68 | +/** |
| 69 | + * @param {number[]} nums |
| 70 | + * @return {number} |
| 71 | + */ |
| 72 | +var reductionOperations = function(nums) { |
| 73 | + nums.sort((a, b) => a - b); |
| 74 | + var uniqueNums = []; |
| 75 | + for (var i = 0; i < nums.length; i++) { |
| 76 | + if (nums[i] === nums[i - 1]) { |
| 77 | + uniqueNums[uniqueNums.length - 1]++; |
| 78 | + } else { |
| 79 | + uniqueNums.push(1); |
| 80 | + } |
| 81 | + } |
| 82 | + var res = 0; |
| 83 | + while (uniqueNums.length > 1) { |
| 84 | + var last = uniqueNums.pop(); |
| 85 | + res += last; |
| 86 | + uniqueNums[uniqueNums.length - 1] += last; |
| 87 | + } |
| 88 | + return res; |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +**Explain:** |
| 93 | + |
| 94 | +nope. |
| 95 | + |
| 96 | +**Complexity:** |
| 97 | + |
| 98 | +* Time complexity : O(n * log(n)). |
| 99 | +* Space complexity : O(n). |
0 commit comments