• 1200.Minimum Absolute Difference


    The description of the problem

    Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements. 
    return a list of parirs in ascending order (with respect to pairs), each pair [a, b] follows.
    1. a, b are from arr
    2. a < b
    3. b - a equals to the minimum absolute difference of any two elements in arr.
    
    • 1
    • 2
    • 3
    • 4
    • 5

    an example

    Input: arr = [4,2,1,3]
    Output: [[1,2],[2,3],[3,4]]
    Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
    
    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/minimum-absolute-difference
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    The solution 1

    The intuition

    Leverage the loop nesting to traveral all the diffVal and store them into a map and log the minimum difference value. In the end, sort out the return nesting loop.

    The corresponding codes

    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <climits>
    #include <map>
    using namespace std;
    class Solution {
    public:
        vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
            vector<vector<int>> res;
            map<int, vector<vector<int>>> m;
            int minDiff = INT_MAX;
            for (int i = 0; i < arr.size(); i++) {
                for (int j = i + 1; j < arr.size(); j++) {
                    int diff = abs(arr[i] - arr[j]);
                    minDiff = min(minDiff, diff);
                    vector<int> t;
                    if (arr[i] < arr[j]) {
                        t.push_back(arr[i]);
                        t.push_back(arr[j]);
                    } else {
                        t.push_back(arr[j]);
                        t.push_back(arr[i]);
                    }
                    m[diff].push_back(t);
                }
            }
            res = m[minDiff];
            // sort res by first element
            sort(res.begin(), res.end(), [](vector<int>& a, vector<int>& b) {
                return a[0] < b[0];
            });
            return res;
            
        }
    };
    int main() 
    {
        Solution s;
        vector<int> arr = {4, 2, 1, 3};
        vector<vector<int>> res = s.minimumAbsDifference(arr);
        for (auto& v : res) {
            for (auto& i : v) {
                cout << i << " ";
            }
            cout << endl;
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    The result

    在这里插入图片描述

    The solution 2

    firstly sort out the elements in arr in asecending order. traversal the arr to find the minimum difference value. Lastly, traversal the second time.

    The codes

    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <climits>
    #include <map>
    using namespace std;
    class Solution {
    public:
        vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
            vector<vector<int>> res;
            int minDiff = INT_MAX;
            sort(arr.begin(), arr.end());
            for (int i = 0; i < arr.size() - 1; i++) {
                int diff = arr[i + 1] - arr[i];
                minDiff = min(minDiff, diff);
            }
            for (auto it = arr.begin(); it != arr.end() - 1; it++) {
                if (abs(*(it + 1) - *it) == minDiff) {
                    res.push_back({*it, *(it + 1)});
                }
            }
            return res;
        }
    };
    int main() 
    {
        Solution s;
        vector<int> arr = {4, 2, 1, 3};
        vector<vector<int>> res = s.minimumAbsDifference(arr);
        for (auto& v : res) {
            for (auto& i : v) {
                cout << i << " ";
            }
            cout << endl;
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    The corresponding results

    在这里插入图片描述

    The more optimized solution

    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <climits>
    #include <map>
    using namespace std;
    class Solution {
    public:
        vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
            vector<vector<int>> res;
            int minDiff = INT_MAX;
            sort(arr.begin(), arr.end());
            for (int i = 0; i < arr.size() - 1; i++) {
                int diff = arr[i + 1] - arr[i];
                if (diff < minDiff) {
                    minDiff = diff;
                    res.clear();
                    res.push_back({arr[i], arr[i + 1]});
                } else if (diff == minDiff) {
                    res.push_back({arr[i], arr[i + 1]});
                }
            }
            return res;
        }
    };
    int main() 
    {
        Solution s;
        vector<int> arr = {4, 2, 1, 3};
        vector<vector<int>> res = s.minimumAbsDifference(arr);
        for (auto& v : res) {
            for (auto& i : v) {
                cout << i << " ";
            }
            cout << endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    The corresponding result

    在这里插入图片描述

  • 相关阅读:
    Python每日一练——第9天:选择排序【含动图展示】
    Ceph入门到精通-生产日志级别设置
    ROS2——动作(十一)
    智能家居设备安全分析技术综述
    如何申请公司邓白氏编码(D-U-N-S Number)
    python字典和集合
    ubuntu在线直接升级
    Android编译C/C++代码,编译出的so文件给别的项目用,CMakeLists.txt编译,请放弃Android.mk!
    未来社区的人车房隐私数据权属确认方法
    重庆人文科技学院建立“软件安全产学研基地”,推动西南地区软件安全发展
  • 原文地址:https://blog.csdn.net/weixin_38396940/article/details/125633474