Minimum Operations to Make All Array Elements Equal Problem
Description
LeetCode Problem 2602.
You are given an array nums consisting of positive integers.
You are also given an integer array queries of size m. For the i^th query, you want to make all of the elements of nums equal to queries[i]. You can perform the following operation on the array any number of times:
- Increase or decrease an element of the array by
1.
Return an array answer of size m where answer[i] is the minimum number of operations to make all elements of nums equal to queries[i].
Note that after each query the array is reset to its original state.
Example 1:
Input: nums = [3,1,6,8], queries = [1,5]
Output: [14,10]
Explanation: For the first query we can do the following operations:
- Decrease nums[0] 2 times, so that nums = [1,1,6,8].
- Decrease nums[2] 5 times, so that nums = [1,1,1,8].
- Decrease nums[3] 7 times, so that nums = [1,1,1,1].
So the total number of operations for the first query is 2 + 5 + 7 = 14.
For the second query we can do the following operations:
- Increase nums[0] 2 times, so that nums = [5,1,6,8].
- Increase nums[1] 4 times, so that nums = [5,5,6,8].
- Decrease nums[2] 1 time, so that nums = [5,5,5,8].
- Decrease nums[3] 3 times, so that nums = [5,5,5,5].
So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
Example 2:
Input: nums = [2,9,6,3], queries = [10]
Output: [20]
Explanation: We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
Constraints:
n == nums.lengthm == queries.length1 <= n, m <= 10^51 <= nums[i], queries[i] <= 10^9
Solution
Approach: Sort + prefix sum + binary search
Sort nums and build the prefix sums s, where s[i] is the sum of the first i elements.
For a query x, the elements larger than x must be decreased and the smaller ones increased. Use binary search to find i, the index of the first element greater than x: the n - i larger elements cost s[n] - s[i] - (n - i) * x. Likewise find j, the index of the first element not smaller than x: the j smaller elements cost x * j - s[j]. The answer to the query is the sum of the two costs.
Complexity: time O((n + m) log n), space O(n), where n and m are the lengths of nums and queries.
from bisect import bisect_left
from itertools import accumulate
class Solution:
def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:
nums.sort()
s = list(accumulate(nums, initial=0))
ans = []
for x in queries:
i = bisect_left(nums, x + 1)
t = s[-1] - s[i] - (len(nums) - i) * x
i = bisect_left(nums, x)
t += x * i - s[i]
ans.append(t)
return ans