Similar Problems
Similar Problems not available
Minimum Total Cost To Make Arrays Unequal - Leetcode Solution
LeetCode: Minimum Total Cost To Make Arrays Unequal Leetcode Solution
Difficulty: Hard
Topics: greedy hash-table array
Problem Statement:
You are given n arrays a1,a2,…,an, where each ai contains ni integers.
You can perform the following operation any number of times:
Choose any two arrays (say a and b) with the same number of elements (let it be n). Choose any two corresponding elements (say a[i] and b[i]). Decrement a[i] and increment b[i]. Compute the total cost by taking the sum of absolute differences of all corresponding pairs of elements of all arrays.
The goal is to minimize the total cost. Determine the minimum total cost.
Solution:
The given problem can be solved by the following steps:
- Count the total number of integers in the n arrays and check if they can be divided into n arrays such that each array contains the same number of integers. If not, return -1.
- Sort all the arrays in increasing order.
- Compute the median array (the array which has the median value of all the arrays).
- Compute the absolute difference between each element of the median array and the corresponding element of other arrays to get the desired output.
Let’s discuss each step in detail.
-
Check if it is possible to divide the total number of integers in the n arrays into n equal parts: If n does not divide the total number of integers in the n arrays evenly, then it is not possible to perform the given operation and hence the solution is not possible. Eg: if there are 4 arrays (n=4) and the total number of integers across the arrays is 10, then we cannot divide 10 integers into 4 equal parts.
-
Sort all the arrays in increasing order: Sorting the given arrays will help us in computing the median array in the next step. Bundling the corresponding elements of each array from low to high will also help in minimizing the absolute differences in the next step.
-
Compute the median array: The median array is the array with the value equal to the median element of all the arrays. There are a couple of ways to compute the median element. We can either sort all the elements and pick the middle element or we can use the two-pointer method. In the two-pointer method, we start with the very first element of each array and move towards the middle. Once we reach the middle, we can stop and consider the array that has the median value. If the number of integers is even, then we can pick any of the two arrays that have the middle values.
-
Compute the absolute difference: Once we have the median array, we can compute the absolute difference between each element of the median array and the corresponding element of other arrays. We will choose the absolute difference that leads to the minimum cost. The minimum cost can be computed by summing up all the absolute differences.
Code:
def min_moving_cost(arr): # count total number of integers across all arrays total_integers = sum([len(a) for a in arr]) # check if total number of integers can be divided into n arrays if total_integers % len(arr) != 0: return -1 # sort arrays in increasing order arr.sort() # compute median array median = [] mid = total_integers // (2 * len(arr)) i, j = 0, mid for k in range(mid): if arr[i][k] < arr[-(i+1)][-(k+1)]: median.append(arr[-(i+1)][-(k+1)]) arr[i][k], arr[-(i+1)][-(k+1)] = arr[-(i+1)][-(k+1)], arr[i][k] else: median.append(arr[i][k]) arr[i][k], arr[-(i+1)][-(k+1)] = arr[-(i+1)][-(k+1)], arr[i][k] i += 1 # compute absolute difference cost = 0 for a in arr: for i in range(len(a)): cost += abs(median[i] - a[i]) return cost
test the code
arr1 = [[1,2,3]] arr2 = [[4,5,6]] arr3 = [[0,0,1],[99,49,12],[11,11,11], [1, 1, 2]] print(min_moving_cost(arr1)) # should return 3 print(min_moving_cost(arr2)) # should return 3 print(min_moving_cost(arr3)) # should return 137
Minimum Total Cost To Make Arrays Unequal Solution Code
1