Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit deac073

Browse files
committedJan 21, 2025
Add solution #1200
1 parent fa4a583 commit deac073

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@
322322
1108|[Defanging an IP Address](./1108-defanging-an-ip-address.js)|Easy|
323323
1122|[Relative Sort Array](./1122-relative-sort-array.js)|Easy|
324324
1189|[Maximum Number of Balloons](./1189-maximum-number-of-balloons.js)|Easy|
325+
1200|[Minimum Absolute Difference](./1200-minimum-absolute-difference.js)|Easy|
325326
1206|[Design Skiplist](./1206-design-skiplist.js)|Hard|
326327
1207|[Unique Number of Occurrences](./1207-unique-number-of-occurrences.js)|Easy|
327328
1208|[Get Equal Substrings Within Budget](./1208-get-equal-substrings-within-budget.js)|Medium|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 1200. Minimum Absolute Difference
3+
* https://leetcode.com/problems/minimum-absolute-difference/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of distinct integers arr, find all pairs of elements with the minimum absolute
7+
* difference of any two elements.
8+
*
9+
* Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows:
10+
* - a, b are from arr
11+
* - a < b
12+
* - b - a equals to the minimum absolute difference of any two elements in arr
13+
*/
14+
15+
/**
16+
* @param {number[]} arr
17+
* @return {number[][]}
18+
*/
19+
var minimumAbsDifference = function(arr) {
20+
arr.sort((a, b) => a - b);
21+
22+
const min = arr.reduce((m, n, i) => Math.min(m, n - (arr[i - 1] ?? -Infinity)), Infinity);
23+
return arr.reduce((result, n, i) => {
24+
if (min === n - arr[i - 1]) {
25+
result.push([arr[i - 1], n]);
26+
}
27+
return result;
28+
}, []);
29+
};

0 commit comments

Comments
 (0)
Please sign in to comment.