3424. Minimum Cost to Make Arrays Identical LeetCode Solution
In this guide, you will get 3424. Minimum Cost to Make Arrays Identical LeetCode Solution with the best time and space complexity. The solution to Minimum Cost to Make Arrays Identical problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Cost to Make Arrays Identical solution in C++
- Minimum Cost to Make Arrays Identical solution in Java
- Minimum Cost to Make Arrays Identical solution in Python
- Additional Resources
Problem Statement of Minimum Cost to Make Arrays Identical
You are given two integer arrays arr and brr of length n, and an integer k. You can perform the following operations on arr any number of times:
Split arr into any number of contiguous subarrays and rearrange these subarrays in any order. This operation has a fixed cost of k.
Choose any element in arr and add or subtract a positive integer x to it. The cost of this operation is x.
Return the minimum total cost to make arr equal to brr.
Example 1:
Input: arr = [-7,9,5], brr = [7,-2,-5], k = 2
Output: 13
Explanation:
Split arr into two contiguous subarrays: [-7] and [9, 5] and rearrange them as [9, 5, -7], with a cost of 2.
Subtract 2 from element arr[0]. The array becomes [7, 5, -7]. The cost of this operation is 2.
Subtract 7 from element arr[1]. The array becomes [7, -2, -7]. The cost of this operation is 7.
Add 2 to element arr[2]. The array becomes [7, -2, -5]. The cost of this operation is 2.
The total cost to make the arrays equal is 2 + 2 + 7 + 2 = 13.
Example 2:
Input: arr = [2,1], brr = [2,1], k = 0
Output: 0
Explanation:
Since the arrays are already equal, no operations are needed, and the total cost is 0.
Constraints:
1 <= arr.length == brr.length <= 105
0 <= k <= 2 * 1010
-105 <= arr[i] <= 105
-105 <= brr[i] <= 105
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
3424. Minimum Cost to Make Arrays Identical LeetCode Solution in C++
class Solution {
public:
long long minCost(vector<int>& arr, vector<int>& brr, long long k) {
return min(noSortCost(arr, brr), sortCost(arr, brr) + k);
}
private:
long long noSortCost(const vector<int>& arr, const vector<int>& brr) {
long res = 0;
for (int i = 0; i < arr.size(); ++i)
res += abs(arr[i] - brr[i]);
return res;
}
long long sortCost(vector<int>& arr, vector<int>& brr) {
ranges::sort(arr);
ranges::sort(brr);
long res = 0;
for (int i = 0; i < arr.size(); ++i)
res += abs(arr[i] - brr[i]);
return res;
}
};
/* code provided by PROGIEZ */
3424. Minimum Cost to Make Arrays Identical LeetCode Solution in Java
class Solution {
public long minCost(int[] arr, int[] brr, long k) {
return Math.min(noSortCost(arr, brr), sortCost(arr, brr) + k);
}
private long noSortCost(int[] arr, int[] brr) {
long res = 0;
for (int i = 0; i < arr.length; ++i)
res += Math.abs(arr[i] - brr[i]);
return res;
}
private long sortCost(int[] arr, int[] brr) {
Arrays.sort(arr);
Arrays.sort(brr);
long res = 0;
for (int i = 0; i < arr.length; ++i)
res += Math.abs(arr[i] - brr[i]);
return res;
}
}
// code provided by PROGIEZ
3424. Minimum Cost to Make Arrays Identical LeetCode Solution in Python
class Solution:
def minCost(self, arr: list[int], brr: list[int], k: int) -> int:
def cost(arr: list[int], brr: list[int]) -> int:
return sum(abs(a - b) for a, b in zip(arr, brr))
return min(cost(arr, brr), cost(sorted(arr), sorted(brr)) + k)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.