Skip to content

Latest commit

 

History

History
260 lines (223 loc) · 6.72 KB

File metadata and controls

260 lines (223 loc) · 6.72 KB

中文文档

Description

You are given two positive integer arrays nums1 and nums2, both of length n.

The absolute sum difference of arrays nums1 and nums2 is defined as the sum of |nums1[i] - nums2[i]| for each 0 <= i < n (0-indexed).

You can replace at most one element of nums1 with any other element in nums1 to minimize the absolute sum difference.

Return the minimum absolute sum difference after replacing at most one element in the array nums1. Since the answer may be large, return it modulo 109 + 7.

|x| is defined as:

  • x if x >= 0, or
  • -x if x < 0.

 

Example 1:

Input: nums1 = [1,7,5], nums2 = [2,3,5]
Output: 3
Explanation: There are two possible optimal solutions:
- Replace the second element with the first: [1,7,5] => [1,1,5], or
- Replace the second element with the third: [1,7,5] => [1,5,5].
Both will yield an absolute sum difference of |1-2| + (|1-3| or |5-3|) + |5-5| = 3.

Example 2:

Input: nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]
Output: 0
Explanation: nums1 is equal to nums2 so no replacement is needed. This will result in an 
absolute sum difference of 0.

Example 3:

Input: nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
Output: 20
Explanation: Replace the first element with the second: [1,10,4,4,2,7] => [10,10,4,4,2,7].
This yields an absolute sum difference of |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20

 

Constraints:

  • n == nums1.length
  • n == nums2.length
  • 1 <= n <= 105
  • 1 <= nums1[i], nums2[i] <= 105

Solutions

Binary search.

Python3

class Solution:
    def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int:
        diff = [abs(a - b) for a, b in zip(nums1, nums2)]
        mod = 10**9 + 7
        s = sum(diff)
        if s == 0:
            return 0
        nums1.sort()
        n = len(nums1)
        mx = 0
        for i, b in enumerate(nums2):
            d = diff[i]
            if d == 0:
                continue
            idx = bisect_left(nums1, b)
            a1 = a2 = 10**6
            if idx != n:
                a1 = nums1[idx]
            if idx:
                a2 = nums1[idx - 1]
            c = min(abs(b - a1), abs(b - a2))
            mx = max(mx, d - c)
        return (s - mx) % mod

Java

class Solution {
    private static final int MOD = (int) 1e9 + 7;

    public int minAbsoluteSumDiff(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int[] diff = new int[n];
        int s = 0;
        for (int i = 0; i < n; ++i) {
            diff[i] = Math.abs(nums1[i] - nums2[i]);
            s = (s + diff[i]) % MOD;
        }
        if (s == 0) {
            return 0;
        }
        Arrays.sort(nums1);
        int mx = 0;
        for (int i = 0; i < n; ++i) {
            int d = diff[i];
            if (d == 0) {
                continue;
            }
            int b = nums2[i];
            int idx = search(nums1, b);
            int a1 = 1000000, a2 = 1000000;
            if (idx != n) {
                a1 = nums1[idx];
            }
            if (idx != 0) {
                a2 = nums1[idx - 1];
            }
            int c = Math.min(Math.abs(b - a1), Math.abs(b - a2));
            mx = Math.max(mx, d - c);
        }
        return (s - mx + MOD) % MOD;
    }

    private int search(int[] nums, int x) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = (left + right) >> 1;
            if (nums[mid] >= x) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
}

C++

class Solution {
public:
    int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
        const int mod = 1e9 + 7;
        int n = nums1.size();
        vector<int> diff(n);
        int s = 0;
        for (int i = 0; i < n; ++i) {
            diff[i] = abs(nums1[i] - nums2[i]);
            s = (s + diff[i]) % mod;
        }
        if (s == 0) return 0;
        sort(nums1.begin(), nums1.end());
        int mx = 0;
        for (int i = 0; i < n; ++i) {
            int d = diff[i];
            if (d == 0) continue;
            int b = nums2[i];
            int idx = lower_bound(nums1.begin(), nums1.end(), b) - nums1.begin();
            int a1 = 1e6, a2 = 1e6;
            if (idx != n) a1 = nums1[idx];
            if (idx != 0) a2 = nums1[idx - 1];
            int c = min(abs(b - a1), abs(b - a2));
            mx = max(mx, d - c);
        }
        return (s - mx + mod) % mod;
    }
};

Go

func minAbsoluteSumDiff(nums1 []int, nums2 []int) int {
	mod := int(1e9) + 7
	n := len(nums1)
	diff := make([]int, n)
	s := 0
	for i := 0; i < n; i++ {
		diff[i] = abs(nums1[i] - nums2[i])
		s = (s + diff[i]) % mod
	}
	if s == 0 {
		return 0
	}
	sort.Ints(nums1)
	mx := 0
	for i, b := range nums2 {
		d := diff[i]
		if d == 0 {
			continue
		}
		idx := search(nums1, b)
		a1, a2 := 1000000, 1000000
		if idx != n {
			a1 = nums1[idx]
		}
		if idx != 0 {
			a2 = nums1[idx-1]
		}
		c := min(abs(b-a1), abs(b-a2))
		mx = max(mx, d-c)
	}
	return (s - mx + mod) % mod
}

func search(nums []int, x int) int {
	left, right := 0, len(nums)
	for left < right {
		mid := (left + right) >> 1
		if nums[mid] >= x {
			right = mid
		} else {
			left = mid + 1
		}
	}
	return left
}

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

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

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

...