LeetCode每日一题(2499. Minimum Total Cost to Make Arrays Unequal)

You are given two 0-indexed integer arrays nums1 and nums2, of equal length n.

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.

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.

Return the minimum total cost such that nums1 and nums2 satisfy the above condition. In case it is not possible, return -1.

Example 1:

Input: nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]
Output: 10

Explanation:
One of the ways we can perform the operations is:

  • Swap values at indices 0 and 3, incurring cost = 0 + 3 = 3. Now, nums1 = [4,2,3,1,5]
  • Swap values at indices 1 and 2, incurring cost = 1 + 2 = 3. Now, nums1 = [4,3,2,1,5].
  • Swap values at indices 0 and 4, incurring cost = 0 + 4 = 4. Now, nums1 =[5,3,2,1,4].
    We can see that for each index i, nums1[i] != nums2[i]. The cost required here is 10.
    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.

Example 2:

Input: nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]
Output: 10

Explanation:
One of the ways we can perform the operations is:

  • Swap values at indices 2 and 3, incurring cost = 2 + 3 = 5. Now, nums1 = [2,2,1,2,3].
  • Swap values at indices 1 and 4, incurring cost = 1 + 4 = 5. Now, nums1 = [2,3,1,2,2].
    The total cost needed here is 10, which is the minimum possible.

Example 3:

Input: nums1 = [1,2,2], nums2 = [1,2,2]
Output: -1

Explanation:
It can be shown that it is not possible to satisfy the given conditions irrespective of the number of operations we perform.
Hence, we return -1.

Constraints:

  • n == nums1.length == nums2.length
  • 1 <= n <= 10^5
  • 1 <= nums1[i], nums2[i] <= n

抄作业抄明白的, 原文在这里, 大家有兴趣的去看吧

我们要交换的位置 i 一定是 nums1[i] == nums2[i], 把这些要交换数字的总数和频次统计出来, 如果, 最大出现频次 _ 2 <= 总数, 则证明通过这些数字内部交换就可以实现, 如果, 最大出现频次 _ 2 > 总数, 我们就要从外面那些 nums1[i] != num2[i]的位置拉一部分进来进行交换才有可能实现。



impl Solution {
    pub fn minimum_total_cost(mut nums1: Vec<i32>, nums2: Vec<i32>) -> i64 {
        let length = nums1.len();
        let mut total = 0;
        let mut freqs = vec![0; length + 1];
        let mut ans = 0;
        for (i, (n1, n2)) in nums1.iter().zip(&nums2).enumerate() {
            if n1 == n2 {
                total += 1;
                freqs[*n1 as usize] += 1;
                ans += i as i64;
            }
        }
        let (n, most) = freqs
            .into_iter()
            .enumerate()
            .max_by_key(|(_, c)| *c)
            .map(|(i, c)| (i as i32, c))
            .unwrap();
        if total >= most * 2 {
            return ans;
        }
        for i in 0..length {
            if total >= most * 2 {
                break;
            }
            if nums1[i] == nums2[i] {
                continue;
            }
            if n != nums1[i] && n != nums2[i] {
                total += 1;
                ans += i as i64;
            }
        }
        if total >= most * 2 {
            ans
        } else {
            -1
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值