1887 Reduce Operations to Make the Array Elements Equal - Medium
Problem:
Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:
- Find the largest value in
nums. Let its index bei(0-indexed) and its value belargest. If there are multiple elements with the largest value, pick the smallesti. - Find the next largest value in
numsstrictly smaller thanlargest. Let its value benextLargest. - Reduce
nums[i]tonextLargest.
Return the number of operations to make all elements in nums equal.
Example 1:
Input: nums = [5,1,3] Output: 3 Explanation: It takes 3 operations to make all elements in nums equal:
- largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
- largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
- largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
Example 2:
Input: nums = [1,1,1] Output: 0 Explanation: All elements in nums are already equal.
Example 3:
Input: nums = [1,1,2,2,3] Output: 4 Explanation: It takes 4 operations to make all elements in nums equal:
- largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
- largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
- largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
- largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
Constraints:
1 <= nums.length <= 5 * 1041 <= nums[i] <= 5 * 104
Problem Analysis:
-
Counting Occurrences:
- The solution begins by creating a
Counterobject (counter) to efficiently count the occurrences of each unique element in thenumsarray.
- The solution begins by creating a
-
Sorting Keys:
- The unique elements (keys) and their counts are extracted from the
counterand sorted in descending order based on counts. This sorting is crucial for efficiently processing elements with higher counts first.
- The unique elements (keys) and their counts are extracted from the
-
Accumulating Operations:
- The solution then iterates through the sorted keys, and for each key, it accumulates the count of that key in the
currentvariable. Thecurrentvariable represents the number of operations needed for the current key.
- The solution then iterates through the sorted keys, and for each key, it accumulates the count of that key in the
-
Total Operations:
- The
totalvariable keeps track of the cumulative sum of thecurrentvariable for all keys except the last one. This total represents the minimum number of operations needed to make all array elements equal.
- The
-
Result:
- The final result is the value stored in the
totalvariable, which is the minimum number of operations required.
- The final result is the value stored in the
-
Time Complexity:
- O(N log N) due to the sorting operation.
- The dominant factor in the time complexity is the sorting of the keys based on their counts. Sorting has a time complexity of O(N log N), where N is the length of the input array
nums.
- The dominant factor in the time complexity is the sorting of the keys based on their counts. Sorting has a time complexity of O(N log N), where N is the length of the input array
- O(N log N) due to the sorting operation.
-
Space Complexity:
- O(N) for the
counterandkeyslists.- The
counterdictionary andkeyslist store information about the unique elements and their counts. In the worst case, this space complexity is proportional to the size of the input array. The additional variables (totalandcurrent) use constant space.
- The
- O(N) for the
Solutions:
class Solution:
def reductionOperations(self, nums: List[int]) -> int:
counter = collections.Counter(nums)
keys = sorted(counter.keys(), reverse=True)
total, current = 0, 0
for key in keys[:-1]:
current += counter[key]
total += current
return total
Walter Teng.