3424. Minimum Cost to Make Arrays Identical Medium
1/**
2 * [3424] Minimum Cost to Make Arrays Identical
3 *
4 * 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:
5 *
6 * Split arr into any number of contiguous <span data-keyword="subarray-nonempty">subarrays</span> and rearrange these subarrays in any order. This operation has a fixed cost of k.
7 *
8 * Choose any element in arr and add or subtract a positive integer x to it. The cost of this operation is x.
9 *
10 *
11 * Return the minimum total cost to make arr equal to brr.
12 *
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">arr = [-7,9,5], brr = [7,-2,-5], k = 2</span>
16 * Output: <span class="example-io">13</span>
17 * Explanation:
18 *
19 * Split arr into two contiguous subarrays: [-7] and [9, 5] and rearrange them as [9, 5, -7], with a cost of 2.
20 * Subtract 2 from element arr[0]. The array becomes [7, 5, -7]. The cost of this operation is 2.
21 * Subtract 7 from element arr[1]. The array becomes [7, -2, -7]. The cost of this operation is 7.
22 * Add 2 to element arr[2]. The array becomes [7, -2, -5]. The cost of this operation is 2.
23 *
24 * The total cost to make the arrays equal is 2 + 2 + 7 + 2 = 13.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">arr = [2,1], brr = [2,1], k = 0</span>
29 * Output: <span class="example-io">0</span>
30 * Explanation:
31 * Since the arrays are already equal, no operations are needed, and the total cost is 0.
32 * </div>
33 *
34 * Constraints:
35 *
36 * 1 <= arr.length == brr.length <= 10^5
37 * 0 <= k <= 2 * 10^10
38 * -10^5 <= arr[i] <= 10^5
39 * -10^5 <= brr[i] <= 10^5
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/minimum-cost-to-make-arrays-identical/
45// discuss: https://leetcode.com/problems/minimum-cost-to-make-arrays-identical/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn min_cost(arr: Vec<i32>, brr: Vec<i32>, k: i64) -> i64 {
51
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_3424() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.