列表

详情


1818. 绝对差值和

给你两个正整数数组 nums1nums2 ,数组的长度都是 n

数组 nums1nums2绝对差值和 定义为所有 |nums1[i] - nums2[i]|0 <= i < n)的 总和下标从 0 开始)。

你可以选用 nums1 中的 任意一个 元素来替换 nums1 中的 至多 一个元素,以 最小化 绝对差值和。

在替换数组 nums1 中最多一个元素 之后 ,返回最小绝对差值和。因为答案可能很大,所以需要对 109 + 7 取余 后返回。

|x| 定义为:

 

示例 1:

输入:nums1 = [1,7,5], nums2 = [2,3,5]
输出:3
解释:有两种可能的最优方案:
- 将第二个元素替换为第一个元素:[1,7,5] => [1,1,5] ,或者
- 将第二个元素替换为第三个元素:[1,7,5] => [1,5,5]
两种方案的绝对差值和都是 |1-2| + (|1-3| 或者 |5-3|) + |5-5| = 3

示例 2:

输入:nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]
输出:0
解释:nums1 和 nums2 相等,所以不用替换元素。绝对差值和为 0

示例 3

输入:nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
输出:20
解释:将第一个元素替换为第二个元素:[1,10,4,4,2,7] => [10,10,4,4,2,7]
绝对差值和为 |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) { } };

python3 解法, 执行用时: 320 ms, 内存消耗: 29.3 MB, 提交时间: 2023-09-18 15:05:10

class Solution:
    def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int:
        a = sorted(nums1)
        best, total, n = 0, 0, len(nums1)
        for x, y in zip(nums1, nums2):
            cur = abs(x - y)
            total += cur
            idx = bisect.bisect_left(a, y)
            if idx < n:
                best = max(best, cur - (a[idx] - y))
            if idx > 0:
                best = max(best, cur - (y - a[idx - 1]))
        
        return (total - best) % (10**9 + 7)

golang 解法, 执行用时: 168 ms, 内存消耗: 8.6 MB, 提交时间: 2023-09-18 15:02:05

func minAbsoluteSumDiff(nums1, nums2 []int) int {
    rec := append(sort.IntSlice(nil), nums1...)
    rec.Sort()
    sum, maxn, n := 0, 0, len(nums1)
    for i, v := range nums2 {
        diff := abs(nums1[i] - v)
        sum += diff
        j := rec.Search(v)
        if j < n {
            maxn = max(maxn, diff-(rec[j]-v))
        }
        if j > 0 {
            maxn = max(maxn, diff-(v-rec[j-1]))
        }
    }
    return (sum - maxn) % (1e9 + 7)
}

func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

javascript 解法, 执行用时: 208 ms, 内存消耗: 54.1 MB, 提交时间: 2023-09-18 15:01:42

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var minAbsoluteSumDiff = function(nums1, nums2) {
    const MOD = 1000000007;
    const n = nums1.length;
    const rec = [...nums1];
    rec.sort((a, b) => a - b);
    let sum = 0, maxn = 0;
    for (let i = 0; i < n; i++) {
        const diff = Math.abs(nums1[i] - nums2[i]);
        sum = (sum + diff) % MOD;
        const j = binarySearch(rec, nums2[i]);
        if (j < n) {
            maxn = Math.max(maxn, diff - (rec[j] - nums2[i]));
        }
        if (j > 0) {
            maxn = Math.max(maxn, diff - (nums2[i] - rec[j - 1]));
        }
    }
    return (sum - maxn + MOD) % MOD;
};

const binarySearch = (rec, target) => {
    let low = 0, high = rec.length - 1;
    if (rec[high] < target) {
        return high + 1;
    }
    while (low < high) {
        const mid = Math.floor((high - low) / 2) + low;
        if (rec[mid] < target) {
            low = mid + 1;
        } else {
            high = mid;
        }
    }
    return low;
}

java 解法, 执行用时: 71 ms, 内存消耗: 59.3 MB, 提交时间: 2023-09-18 15:01:28

class Solution {
    public int minAbsoluteSumDiff(int[] nums1, int[] nums2) {
        final int MOD = 1000000007;
        int n = nums1.length;
        int[] rec = new int[n];
        System.arraycopy(nums1, 0, rec, 0, n);
        Arrays.sort(rec);
        int sum = 0, maxn = 0;
        for (int i = 0; i < n; i++) {
            int diff = Math.abs(nums1[i] - nums2[i]);
            sum = (sum + diff) % MOD;
            int j = binarySearch(rec, nums2[i]);
            if (j < n) {
                maxn = Math.max(maxn, diff - (rec[j] - nums2[i]));
            }
            if (j > 0) {
                maxn = Math.max(maxn, diff - (nums2[i] - rec[j - 1]));
            }
        }
        return (sum - maxn + MOD) % MOD;
    }

    public int binarySearch(int[] rec, int target) {
        int low = 0, high = rec.length - 1;
        if (rec[high] < target) {
            return high + 1;
        }
        while (low < high) {
            int mid = (high - low) / 2 + low;
            if (rec[mid] < target) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
}

cpp 解法, 执行用时: 208 ms, 内存消耗: 62.9 MB, 提交时间: 2023-09-18 15:01:15

// 排序 + 二分
class Solution {
public:
    static constexpr int mod = 1'000'000'007;

    int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
        vector<int> rec(nums1);
        sort(rec.begin(), rec.end());
        int sum = 0, maxn = 0;
        int n = nums1.size();
        for (int i = 0; i < n; i++) {
            int diff = abs(nums1[i] - nums2[i]);
            sum = (sum + diff) % mod;
            int j = lower_bound(rec.begin(), rec.end(), nums2[i]) - rec.begin();
            if (j < n) {
                maxn = max(maxn, diff - (rec[j] - nums2[i]));
            }
            if (j > 0) {
                maxn = max(maxn, diff - (nums2[i] - rec[j - 1]));
            }
        }
        return (sum - maxn + mod) % mod;
    }
};

上一题