[LeetCode]#1200. Minimum Absolute Difference

Environment: Python 3.8

Key technique: if, append

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:

Analysis:

  1. Sort arr.
  2. st=abs(arr[1]-arr[0])
  3. if st == abs(arr[i]-arr[i-1]), add to and[arr[i-1],arr[i]]
  4. if st > abs(arr[i]-arr[i-1]), replace new st and ans.
  5. Return ans

Solution:

Submissions:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store