preface

I am very glad to participate in this activity. After all, I usually brush questions and submit them, but I do not use words to record and review the process and ideas. This activity can cultivate a habit of thinking, recording, reviewing and optimizing. The content of my activity should be mostly Easy, and there may be medium. The main reason is that I am too bad for medium. I have to think about it for a long time, so I will not write it to show my shame. I hope you can forgive me.

Today already wrote a brush [LeetCode topic action] | brush question clock in, according to an official advice is the best day, but I in overtime, there is something wrong with the backend, so at the moment idle is idle, I do not receive the official suggestion formation (manual).

Topic describes

This is LeetCode1200 1200. Minimum absolute difference.

I give you an integer array arr, where each element is different.

Find all pairs of elements with the smallest absolute difference and return them in ascending order.

Example 1:

Input: arr = [4,2,1,3] output: [[1,2],[2,3],[3,4]Copy the code

Example 2:

Input: arr = [1,3,6,10,15] output: [[1,3]Copy the code

Example 3:

Input: arr = [3, 8-10,23,19-4-14, 27] output: [[- 14, 10], [19, 23], [23, 27]]Copy the code

Tip:

  • 2 <= arr.length <= 10^5

  • -10^6 <= arr[i] <= 10^6

Their thinking

Given an integer array arr, each element in arR is different, we are asked to return all pairs of elements with the smallest absolute difference in ascending order. See this first thought of the given integer array ARR ascending sort, so that we can find the difference sort. Then set the double Pointers left and right to be the first and second elements of the array, then evaluate the difference flag, and set a Max as a reference.

If flag is less than Max, it means that these are the two elements with the smallest difference so far. The result array res should be left blank, and ARr [left] and ARr [right] should be stored in RES as an array, and flag should be assigned to Max for the next comparison.

If flag and Max are equal, continue storing arr[left] and arr[right] in res as an array. Note that this step does not require null res.

Then left++; Right++; The pointer moves to the right until the end of the array, and then returns res.

The problem solving code

Var minimumAbsDifference = function(arr) {var minimumAbsDifference = function(arr) {var minimumAbsDifference = function(arr) {arr.sort((a, b) => a-b) const n = arr.length let left = 0, right = 1, res = [], max = Number.MAX_SAFE_INTEGER; while(right < n) { const flag = arr[right] - arr[left] if (flag < max) { res = [] res.push([arr[left], arr[right]]) max = flag } else if (flag === max) { res.push([arr[left], arr[right]]) } left++; right++; } return res; }Copy the code

conclusion

The method I use is a relatively stupid method, mainly simple and easy to understand, so that you can understand, and then you can go according to their own ideas to get their own solution. It is nice to practice and write without it.

Come on! Good brothers! Please click “like”!!

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign