2499. Minimum Total Cost to Make Arrays Unequal Hard
1/**
2 * [2499] Minimum Total Cost to Make Arrays Unequal
3 *
4 * You are given two 0-indexed integer arrays nums1 and nums2, of equal length n.
5 * In one operation, you can swap the values of any two indices of nums1. The cost of this operation is the sum of the indices.
6 * Find the minimum total cost of performing the given operation any number of times such that nums1[i] != nums2[i] for all 0 <= i <= n - 1 after performing all the operations.
7 * Return the minimum total cost such that nums1 and nums2 satisfy the above condition. In case it is not possible, return -1.
8 *
9 * <strong class="example">Example 1:
10 *
11 * Input: nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]
12 * Output: 10
13 * Explanation:
14 * One of the ways we can perform the operations is:
15 * - Swap values at indices 0 and 3, incurring cost = 0 + 3 = 3. Now, nums1 = [4,2,3,1,5]
16 * - Swap values at indices 1 and 2, incurring cost = 1 + 2 = 3. Now, nums1 = [4,3,2,1,5].
17 * - Swap values at indices 0 and 4, incurring cost = 0 + 4 = 4. Now, nums1 =[5,3,2,1,4].
18 * We can see that for each index i, nums1[i] != nums2[i]. The cost required here is 10.
19 * Note that there are other ways to swap values, but it can be proven that it is not possible to obtain a cost less than 10.
20 *
21 * <strong class="example">Example 2:
22 *
23 * Input: nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]
24 * Output: 10
25 * Explanation:
26 * One of the ways we can perform the operations is:
27 * - Swap values at indices 2 and 3, incurring cost = 2 + 3 = 5. Now, nums1 = [2,2,1,2,3].
28 * - Swap values at indices 1 and 4, incurring cost = 1 + 4 = 5. Now, nums1 = [2,3,1,2,2].
29 * The total cost needed here is 10, which is the minimum possible.
30 *
31 * <strong class="example">Example 3:
32 *
33 * Input: nums1 = [1,2,2], nums2 = [1,2,2]
34 * Output: -1
35 * Explanation:
36 * It can be shown that it is not possible to satisfy the given conditions irrespective of the number of operations we perform.
37 * Hence, we return -1.
38 *
39 *
40 * Constraints:
41 *
42 * n == nums1.length == nums2.length
43 * 1 <= n <= 10^5
44 * 1 <= nums1[i], nums2[i] <= n
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/minimum-total-cost-to-make-arrays-unequal/
50// discuss: https://leetcode.com/problems/minimum-total-cost-to-make-arrays-unequal/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55 pub fn minimum_total_cost(nums1: Vec<i32>, nums2: Vec<i32>) -> i64 {
56
57 }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_2499() {
68 }
69}
70
Back
© 2025 bowen.ge All Rights Reserved.