|
| 1 | +// Source : https://leetcode.com/problems/minimum-absolute-sum-difference/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-04-05 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given two positive integer arrays nums1 and nums2, both of length n. |
| 8 | + * |
| 9 | + * The absolute sum difference of arrays nums1 and nums2 is defined as the sum of |nums1[i] - |
| 10 | + * nums2[i]| for each 0 <= i < n (0-indexed). |
| 11 | + * |
| 12 | + * You can replace at most one element of nums1 with any other element in nums1 to minimize the |
| 13 | + * absolute sum difference. |
| 14 | + * |
| 15 | + * Return the minimum absolute sum difference after replacing at most one element in the array nums1. |
| 16 | + * Since the answer may be large, return it modulo 10^9 + 7. |
| 17 | + * |
| 18 | + * |x| is defined as: |
| 19 | + * |
| 20 | + * x if x >= 0, or |
| 21 | + * -x if x < 0. |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Input: nums1 = [1,7,5], nums2 = [2,3,5] |
| 26 | + * Output: 3 |
| 27 | + * Explanation: There are two possible optimal solutions: |
| 28 | + * - Replace the second element with the first: [1,7,5] => [1,1,5], or |
| 29 | + * - Replace the second element with the third: [1,7,5] => [1,5,5]. |
| 30 | + * Both will yield an absolute sum difference of |1-2| + (|1-3| or |5-3|) + |5-5| = 3. |
| 31 | + * |
| 32 | + * Example 2: |
| 33 | + * |
| 34 | + * Input: nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10] |
| 35 | + * Output: 0 |
| 36 | + * Explanation: nums1 is equal to nums2 so no replacement is needed. This will result in an |
| 37 | + * absolute sum difference of 0. |
| 38 | + * |
| 39 | + * Example 3: |
| 40 | + * |
| 41 | + * Input: nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4] |
| 42 | + * Output: 20 |
| 43 | + * Explanation: Replace the first element with the second: [1,10,4,4,2,7] => [10,10,4,4,2,7]. |
| 44 | + * This yields an absolute sum difference of |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20 |
| 45 | + * |
| 46 | + * Constraints: |
| 47 | + * |
| 48 | + * n == nums1.length |
| 49 | + * n == nums2.length |
| 50 | + * 1 <= n <= 10^5 |
| 51 | + * 1 <= nums1[i], nums2[i] <= 10^5 |
| 52 | + ******************************************************************************************************/ |
| 53 | + |
| 54 | +class Solution { |
| 55 | +public: |
| 56 | + int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) { |
| 57 | + int max=0, idx=0; |
| 58 | + long sum=0; |
| 59 | + int len = nums1.size(); |
| 60 | + for (int i=0; i<len; i++) { |
| 61 | + int m = abs(nums1[i] - nums2[i]); |
| 62 | + if (max < m) { |
| 63 | + max = m; |
| 64 | + idx = i; |
| 65 | + } |
| 66 | + sum += m ; |
| 67 | + } |
| 68 | + int min = max; |
| 69 | + for (int i=0; i<len; i++) { |
| 70 | + int m = abs(nums1[i] - nums2[idx]); |
| 71 | + if (m < min) { |
| 72 | + min = m; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + sum -= (max - min); |
| 77 | + return sum % 1000000007 ; |
| 78 | + } |
| 79 | +}; |
0 commit comments