#3139
Minimum Cost to Equalize Array
hard · 18.7% accepted · 149 likes · top 1%
array · greedy · enumeration
Description
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 i from nums and increase nums[i] by 1 for a cost of cost1.
- Choose two different indices i, j, from nums and increase nums[i] and nums[j] by 1 for a cost of cost2.
Return the minimum cost required to make all elements in the array equal.
Since the answer may be very large, return it modulo 109 + 7.
Solution