{"id":5569,"date":"2019-09-22T01:39:20","date_gmt":"2019-09-22T08:39:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5569"},"modified":"2019-09-22T01:40:03","modified_gmt":"2019-09-22T08:40:03","slug":"leetcode-1200-minimum-absolute-difference","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1200-minimum-absolute-difference\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1200. Minimum Absolute Difference"},"content":{"rendered":"\n<p>Given an&nbsp;array&nbsp;of&nbsp;<strong>distinct<\/strong>&nbsp;integers&nbsp;<code>arr<\/code>, find all pairs of elements with the minimum absolute difference of any two elements.&nbsp;<\/p>\n\n\n\n<p>Return a list of pairs in ascending order(with respect to pairs), each pair&nbsp;<code>[a, b]<\/code>&nbsp;follows<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>a, b<\/code>&nbsp;are from&nbsp;<code>arr<\/code><\/li><li><code>a &lt; b<\/code><\/li><li><code>b - a<\/code>&nbsp;equals to the minimum absolute difference of any two elements in&nbsp;<code>arr<\/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> arr = [4,2,1,3]\n<strong>Output:<\/strong> [[1,2],[2,3],[3,4]]\n<strong>Explanation: <\/strong>The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.<\/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> arr = [1,3,6,10,15]\n<strong>Output:<\/strong> [[1,3]]\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> arr = [3,8,-10,23,19,-4,-14,27]\n<strong>Output:<\/strong> [[-14,-10],[19,23],[23,27]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>-10^6 &lt;= arr[i] &lt;= 10^6<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sorting<\/strong><\/h2>\n\n\n\n<p>The min abs difference could only happen between consecutive numbers in sorted form.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> minimumAbsDifference(vector<int>& arr) {\n    sort(begin(arr), end(arr));\n    vector<vector<int>> ans;\n    int best = INT_MAX;\n    for (int i = 1; i < arr.size(); ++i) {\n      int d = abs(arr[i] - arr[i - 1]);      \n      if (d < best) {\n        ans.clear();\n        best = d;\n      }\n      if (d == best) ans.push_back({arr[i - 1], arr[i]});      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an&nbsp;array&nbsp;of&nbsp;distinct&nbsp;integers&nbsp;arr, find all pairs of elements with the minimum absolute difference of any two elements.&nbsp; Return a list of pairs in ascending order(with respect&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[413,20,222,377,15],"class_list":["post-5569","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-all-pairs","tag-array","tag-easy","tag-onlogn","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5569","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=5569"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5569\/revisions"}],"predecessor-version":[{"id":5572,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5569\/revisions\/5572"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}