FastPrepMinimum Cost to Make Array Equal
Problem · Array

Minimum Cost to Make Array Equal

Learn this problem
HardWalmart logoWalmartFULLTIMEOA

Problem 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[]) → long

Examples

Example 1

nums = [1,3,5,2]cost = [2,3,1,14]return = 8

Making every value 2 costs 2 + 3 + 3 + 0 = 8.

Example 2

nums = [2,2,2,2,2]cost = [4,2,8,1,3]return = 0

The 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.

More Walmart problems

drafts saved locally
public long minCost(int[] nums, int[] cost) {
    // write your code here
}
nums[1,3,5,2]
cost[2,3,1,14]
expected8
checking account