{"id":8325,"date":"2021-04-06T17:27:27","date_gmt":"2021-04-07T00:27:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8325"},"modified":"2021-04-06T17:40:36","modified_gmt":"2021-04-07T00:40:36","slug":"leetcode-1818-minimum-absolute-sum-difference","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1818-minimum-absolute-sum-difference\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1818. Minimum Absolute Sum Difference"},"content":{"rendered":"\n<p>You are given two positive integer arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>, both of length&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>absolute sum difference<\/strong>&nbsp;of arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>&nbsp;is defined as the&nbsp;<strong>sum<\/strong>&nbsp;of&nbsp;<code>|nums1[i] - nums2[i]|<\/code>&nbsp;for each&nbsp;<code>0 &lt;= i &lt; n<\/code>&nbsp;(<strong>0-indexed<\/strong>).<\/p>\n\n\n\n<p>You can replace&nbsp;<strong>at most one<\/strong>&nbsp;element of&nbsp;<code>nums1<\/code>&nbsp;with&nbsp;<strong>any<\/strong>&nbsp;other element in&nbsp;<code>nums1<\/code>&nbsp;to&nbsp;<strong>minimize<\/strong>&nbsp;the absolute sum difference.<\/p>\n\n\n\n<p>Return the&nbsp;<em>minimum absolute sum difference&nbsp;<strong>after<\/strong>&nbsp;replacing at most oneelement in the array&nbsp;<code>nums1<\/code>.<\/em>&nbsp;Since the answer may be large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/p>\n\n\n\n<p><code>|x|<\/code>&nbsp;is defined as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>x<\/code>&nbsp;if&nbsp;<code>x &gt;= 0<\/code>, or<\/li><li><code>-x<\/code>&nbsp;if&nbsp;<code>x &lt; 0<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [1,7,5], nums2 = [2,3,5]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>There are two possible optimal solutions:\n- Replace the second element with the first: [1,<strong>7<\/strong>,5] =&gt; [1,<strong>1<\/strong>,5], or\n- Replace the second element with the third: [1,<strong>7<\/strong>,5] =&gt; [1,<strong>5<\/strong>,5].\nBoth will yield an absolute sum difference of <code>|1-2| + (|1-3| or |5-3|) + |5-5| = <\/code>3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>nums1 is equal to nums2 so no replacement is needed. This will result in an \nabsolute sum difference of 0.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]\n<strong>Output:<\/strong> 20\n<strong>Explanation: <\/strong>Replace the first element with the second: [<strong>1<\/strong>,10,4,4,2,7] =&gt; [<strong>10<\/strong>,10,4,4,2,7].\nThis yields an absolute sum difference of <code>|10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20<\/code>\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums1.length<\/code><\/li><li><code>n == nums2.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Greedy won&#8217;t work, e.g. finding the max diff pair and replace it. Counter example:<br>nums1 = [7, 5], nums2 = [1, -2]<br>pair1 = abs(7 &#8211; 1) = 6<br>pair2 = abs(5 &#8211; (-2)) = 7<br>If we replace 5 with 7, we got pair2&#8242; = abs(7 &#8211; (-2)) = 9 &gt; 7.<\/p>\n\n\n\n<p>Every pair of numbers can be the candidate, we just need to find the closest number for each nums2[i].<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = nums1.size();\n    long ans = 0;\n    int gain = 0;\n    set<int> s(begin(nums1), end(nums1));\n    for (int i = 0; i < n; ++i) {\n      const int diff = abs(nums1[i] - nums2[i]);\n      ans += diff;\n      if (diff <= gain) continue;\n      auto it = s.lower_bound(nums2[i]);      \n      if (it != s.end()) \n        gain = max(gain, diff - abs(*it - nums2[i]));\n      if (it != s.begin()) \n        gain = max(gain, diff - abs(*prev(it) - nums2[i])); \n    }\n    return (ans - gain) % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two positive integer arrays&nbsp;nums1&nbsp;and&nbsp;nums2, both of length&nbsp;n. The&nbsp;absolute sum difference&nbsp;of arrays&nbsp;nums1&nbsp;and&nbsp;nums2&nbsp;is defined as the&nbsp;sum&nbsp;of&nbsp;|nums1[i] &#8211; nums2[i]|&nbsp;for each&nbsp;0 &lt;= i &lt; n&nbsp;(0-indexed). You&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[704,52,177,62],"class_list":["post-8325","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-abs","tag-binary-search","tag-medium","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8325"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8325\/revisions"}],"predecessor-version":[{"id":8327,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8325\/revisions\/8327"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}