Skip to content

Commit f34d9ca

Browse files
committed
Adding solution of 1200.py
1 parent bd81ef4 commit f34d9ca

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Diff for: 1200-1300q/1200.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.
3+
4+
Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows
5+
6+
a, b are from arr
7+
a < b
8+
b - a equals to the minimum absolute difference of any two elements in arr
9+
10+
11+
Example 1:
12+
13+
Input: arr = [4,2,1,3]
14+
Output: [[1,2],[2,3],[3,4]]
15+
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
16+
Example 2:
17+
18+
Input: arr = [1,3,6,10,15]
19+
Output: [[1,3]]
20+
Example 3:
21+
22+
Input: arr = [3,8,-10,23,19,-4,-14,27]
23+
Output: [[-14,-10],[19,23],[23,27]]
24+
25+
26+
Constraints:
27+
28+
2 <= arr.length <= 10^5
29+
-10^6 <= arr[i] <= 10^6
30+
'''
31+
32+
class Solution(object):
33+
def minimumAbsDifference(self, arr):
34+
"""
35+
:type arr: List[int]
36+
:rtype: List[List[int]]
37+
"""
38+
if not arr:
39+
return []
40+
41+
arr.sort()
42+
mindiff = arr[1] - arr[0]
43+
for index in range(2, len(arr)):
44+
mindiff = min(mindiff, (arr[index] - arr[index-1]))
45+
46+
result = []
47+
for index in range(1, len(arr)):
48+
if arr[index] - arr[index-1] == mindiff:
49+
result.append([arr[index-1], arr[index]])
50+
return result
51+

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1111

1212
### LeetCode Algorithm
1313

14+
##### [Problems 1100-1200](./1200-1300q/)
15+
| # | Title | Solution | Difficulty |
16+
|---| ----- | -------- | ---------- |
17+
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Python](./1200-1300q/1200.py)|Easy|
1418

1519
##### [Problems 1100-1200](./1100-1200q/)
1620
| # | Title | Solution | Difficulty |

0 commit comments

Comments
 (0)