Problem · Array
Minimum Cost to Make Array Equal
Learn this problemProblem statement
You are given two arrays nums and cost of equal length. In one operation, you may increase or decrease nums[i] by 1 and pay cost[i].
You may perform any number of operations. Return the minimum total cost needed to make every value in nums equal.
Function
minCost(nums: int[], cost: int[]) → longExamples
Example 1
nums = [1,3,5,2]cost = [2,3,1,14]return = 8Making every value 2 costs 2 + 3 + 3 + 0 = 8.
Example 2
nums = [2,2,2,2,2]cost = [4,2,8,1,3]return = 0The values are already equal.
Constraints
1 <= nums.length == cost.length <= 10^5.1 <= nums[i], cost[i] <= 10^6.- The answer fits in a signed 64-bit integer.