-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimum-absolute-difference.py
More file actions
66 lines (51 loc) · 2.39 KB
/
minimum-absolute-difference.py
File metadata and controls
66 lines (51 loc) · 2.39 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# https://leetcode.com/problems/minimum-absolute-difference/
# Related Topics: Array
# Difficulty: Easy
# Initial thoughts:
# First we need to find the minimum absolute difference between any two elements in the array.
# This can be done by comparing each element with every other element which would take O(n^2)
# or we could sort the array and then compare adjacent elements to find the min difference, which
# takes O(n * log n).
# Then we need to find every pair that has the same absolute difference as the min diff. This could
# also be done in O(n^2) or we could work on the sorted array and only compare adjacent arrays. This
# takes only O(n)
# Time complexity: O(n * log n) where n === len(arr)
# Space complexity: O(n) where n === len(arr) That's for the results array in case every pair in the
# original array is an answer
from typing import List
class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
minimum = float('inf')
for i in range(1, len(arr)):
minimum = min(minimum, arr[i]-arr[i-1])
result = []
for i in range(1, len(arr)):
if arr[i]-arr[i-1] == minimum:
result.append([arr[i-1], arr[i]])
return result
# Optimization:
# We can calculate the result (the correct pairs) in one go while we are
# culculating the minimum difference. When working on a sorted array we can
# be sure that if we find a new minimum difference, the previous results are
# of no value anymore and the pair that created the new min difference also
# belongs to the results array. While checking for the minimum diff, if we
# find a diff that equals our previous minimum diff, we also know that the
# result array until now is correct and this very new pair also belongs to it.
# For other differences that are above the current min diff, we can be sure
# that they are of no value to us.
# Time complexity: O(n * log n)
# Space complexity: O(n)
class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
result = []
minimum = float('inf')
for i in range(1, len(arr)):
temp = arr[i]-arr[i-1]
if temp < minimum:
minimum = arr[i] - arr[i-1]
result = [[arr[i-1], arr[i]]]
elif temp == minimum:
result.append([arr[i-1], arr[i]])
return result