This article is participating in Python Theme Month. See the link for details
describe
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 pairs in ascending order(with respect to pairs), each pair [a, b] follows:
- a, b are from arr
- a < b
- b – a equals to the minimum absolute difference of any two elements in arr
Example 1:
Input: arr =,2,1,3 [4] the 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.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
Note:
2 <= arr.length <= 10^5
-10^6 <= arr[i] <= 10^6
Copy the code
parsing
Find the pairs of values with the smallest difference in arR. If the difference between two adjacent elements is smaller than min_diatance, update min_diatance, update result to empty, and place the two adjacent pairs of values in the arR. If the difference between two adjacent elements is equal to min_diatance, append the two adjacent pairs to result and get the result after traversing the ARR.
answer
class Solution(object):
def minimumAbsDifference(self, arr):
"""
:type arr: List[int]
:rtype: List[List[int]]
"""
arr.sort()
i = 1
min_diatance = float("inf")
result = []
while i < len(arr):
distance = arr[i] - arr[i - 1]
if distance < min_diatance:
min_diatance = distance
result = [[arr[i - 1], arr[i]]]
elif distance == min_diatance:
result.append([arr[i - 1], arr[i]])
i += 1
return result
Copy the code
The results
Given in the Python online submissions with Minimum Absolute Difference. Submissions in Python online submissions for Minimum Absolute Difference.Copy the code
parsing
In addition, you can solve this problem by using the more advanced Python built-in function zip. First sort arR from smallest to largest, then use ZIP to calculate the difference between ARR and ARR [1:], and get the smallest difference m. Then re-traverse the zip chain formed by the combination of ARR and ARr [1:], and find out the value pairs with the difference value of M and put them into the list.
This section describes the functions of ZIP. The zip function takes an iterable object as an argument, packs the corresponding elements of the object into tuples, and returns a list of those tuples. If the iterators do not have the same number of elements, it returns the same list length as the shortest object. Using the * operator, you can unpack the tuple into a list. For example:
Print ([x for x in zip(a,b)]) print([x for x in zip(a,b)] Print ([x for x in zip(*zip(a,b))])Copy the code
Print:
[(1, 4), (2, 5), (3, 6)] [(1, 4), (2, 5), (3, 6)] [(1, 2, 3), (4, 5, 6)"Copy the code
answer
class Solution(object):
def minimumAbsDifference(self, arr):
"""
:type arr: List[int]
:rtype: List[List[int]]
"""
arr.sort()
m = min(j-i for i,j in zip(arr,arr[1:]))
return [[i,j] for i,j in zip(arr,arr[1:]) if j-i==m]
Copy the code
The results
Given in the Python online submissions with Minimum Absolute Difference. Submissions in Python online submissions for Minimum Absolute Difference.Copy the code
Original link: leetcode.com/problems/mi…
Your support is my biggest motivation