3139. Minimum Cost to Equalize Array
3139. Minimum Cost to Equalize Array
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
fromnums
and increasenums[i]
by1
for a cost ofcost1
. - Choose two different indices
i
,j
, fromnums
and increasenums[i]
andnums[j]
by1
for 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:
1 | Input: nums = [4,1], cost1 = 5, cost2 = 2 |
Example 2:
1 | Input: nums = [2,3,3,3,5], cost1 = 2, cost2 = 1 |
Example 3:
1 | Input: nums = [3,5,3], cost1 = 1, cost2 = 3 |
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6
1 <= cost1 <= 10^6
1 <= cost2 <= 10^6
Hints/Notes
- need to iterate through the maxDiff value
Solution
Language: C++
1 | class Solution { |