[
sort
]
Leetcode 1200. Minimum Absolute Difference
Problem statement
https://leetcode.com/problems/minimum-absolute-difference/
Solution
The key idea is that minimum absolut difference can be reached only on pairs of elements which are adjacent in sorted data. So all we need to do is to sort data and then find minimum difference between pairs of adjacent elements and finally choose all pairs with this difference.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def minimumAbsDifference(self, arr):
arr = sorted(arr)
d = 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 == d]