LeetCode1818(2021.7.14)

LC1818. 绝对差值和

class Solution {
public:
    static constexpr int mod = 1e9 + 7;

    int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
        vector<int> rec(nums1);
        sort(rec.begin(), rec.end());
        int sum = 0, maxn = 0, 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;
    }
};

发表评论