Minimum Cost to Equalize Array Problem
Description
LeetCode Problem 3139.
You are given an integer array nums and two integers cost1 and cost2. You are allowed to perform either of the following operations any number of times:
- Choose an index
ifromnumsand increasenums[i]by1for a cost ofcost1. - Choose two different indices
i,j, fromnumsand increasenums[i]andnums[j]by1for a cost ofcost2.
Return the minimum cost required to make all elements in the array equal*.*
Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: nums = [4,1], cost1 = 5, cost2 = 2
Output: 15
Explanation:
The following operations can be performed to make the values equal:
- Increase nums[1] by 1 for a cost of 5. nums becomes [4,2].
- Increase nums[1] by 1 for a cost of 5. nums becomes [4,3].
- Increase nums[1] by 1 for a cost of 5. nums becomes [4,4].
The total cost is 15.
Example 2:
Input: nums = [2,3,3,3,5], cost1 = 2, cost2 = 1
Output: 6
Explanation:
The following operations can be performed to make the values equal:
- Increase nums[0] and nums[1] by 1 for a cost of 1. nums becomes [3,4,3,3,5].
- Increase nums[0] and nums[2] by 1 for a cost of 1. nums becomes [4,4,4,3,5].
- Increase nums[0] and nums[3] by 1 for a cost of 1. nums becomes [5,4,4,4,5].
- Increase nums[1] and nums[2] by 1 for a cost of 1. nums becomes [5,5,5,4,5].
- Increase nums[3] by 1 for a cost of 2. nums becomes [5,5,5,5,5].
The total cost is 6.
Example 3:
Input: nums = [3,5,3], cost1 = 1, cost2 = 3
Output: 4
Explanation:
The following operations can be performed to make the values equal:
- Increase nums[0] by 1 for a cost of 1. nums becomes [4,5,3].
- Increase nums[0] by 1 for a cost of 1. nums becomes [5,5,3].
- Increase nums[2] by 1 for a cost of 1. nums becomes [5,5,4].
- Increase nums[2] by 1 for a cost of 1. nums becomes [5,5,5].
The total cost is 4.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^61 <= cost1 <= 10^61 <= cost2 <= 10^6
Solution
Approach: Greedy + Enumerating the Target
Values can only go up, so the common target t is at least max(nums). For a fixed t, let gap be the total number of increments needed (the sum of t - nums[i]) and big the increments needed by the smallest element.
- If
cost2 >= 2 * cost1, the paired operation never helps: the answer isgap * cost1witht = max(nums). - Otherwise we want as many paired operations as possible. Two increments in one pair must go to different elements, so the smallest element limits us: if
big > gap - big, at mostgap - bigpairs can be formed; otherwise we can formgap / 2pairs. The cost ispairs * cost2 + (gap - 2 * pairs) * cost1.
A larger target can make pairing easier, and it is enough to try every t from max(nums) to 2 * max(nums). Take the smallest cost over all these targets and return it modulo 10^9 + 7 (compare the exact values first, take the modulo only at the end).
Complexity: time O(n + max), space O(1), where max is the largest element.
class Solution:
def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int:
mod = 10**9 + 7
n = len(nums)
mx, mn, total = max(nums), min(nums), sum(nums)
if cost2 >= 2 * cost1:
return (mx * n - total) * cost1 % mod
best = None
for t in range(mx, 2 * mx + 1):
gap = t * n - total # total number of increments
big = t - mn # increments needed by the smallest element
if big > gap - big:
pairs = gap - big
else:
pairs = gap // 2
cost = pairs * cost2 + (gap - 2 * pairs) * cost1
if best is None or cost < best:
best = cost
return best % mod