Skip to content

Commit cd8d0f0

Browse files
authored
Added task 1200.
1 parent 522ddef commit cd8d0f0

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ spotless {
4848
java {
4949
encoding 'UTF-8'
5050
target fileTree(projectDir) {
51-
include '**/src/*.java'
51+
include '**/src/**/*.java'
5252
exclude '**/build/**'
5353
}
5454
importOrder '\\#', '', '*'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1101_1200.s1200_minimum_absolute_difference;
2+
3+
// #Easy #Array #Sorting #2022_02_27_Time_14_ms_(98.30%)_Space_52_MB_(84.02%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public List<List<Integer>> minimumAbsDifference(int[] arr) {
11+
List<List<Integer>> result = new ArrayList<>();
12+
int min = 10000000;
13+
Arrays.sort(arr);
14+
for (int i = 0; i + 1 < arr.length; i++) {
15+
int diff = arr[i + 1] - arr[i];
16+
if (diff <= min) {
17+
if (diff < min) {
18+
min = diff;
19+
result.clear();
20+
}
21+
result.add(Arrays.asList(arr[i], arr[i + 1]));
22+
}
23+
}
24+
return result;
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1200\. Minimum Absolute Difference
2+
3+
Easy
4+
5+
Given an array of **distinct** integers `arr`, find all pairs of elements with the minimum absolute difference of any two elements.
6+
7+
Return a list of pairs in ascending order(with respect to pairs), each pair `[a, b]` follows
8+
9+
* `a, b` are from `arr`
10+
* `a < b`
11+
* `b - a` equals to the minimum absolute difference of any two elements in `arr`
12+
13+
**Example 1:**
14+
15+
**Input:** arr = [4,2,1,3]
16+
17+
**Output:** [[1,2],[2,3],[3,4]]
18+
19+
**Explanation:** The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
20+
21+
**Example 2:**
22+
23+
**Input:** arr = [1,3,6,10,15]
24+
25+
**Output:** [[1,3]]
26+
27+
**Example 3:**
28+
29+
**Input:** arr = [3,8,-10,23,19,-4,-14,27]
30+
31+
**Output:** [[-14,-10],[19,23],[23,27]]
32+
33+
**Constraints:**
34+
35+
* <code>2 <= arr.length <= 10<sup>5</sup></code>
36+
* <code>-10<sup>6</sup> <= arr[i] <= 10<sup>6</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g1101_1200.s1200_minimum_absolute_difference;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ArrayUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void minimumAbsDifference() {
12+
assertThat(
13+
new Solution().minimumAbsDifference(new int[] {4, 2, 1, 3}),
14+
equalTo(ArrayUtils.getLists(new int[][] {{1, 2}, {2, 3}, {3, 4}})));
15+
}
16+
17+
@Test
18+
void minimumAbsDifference2() {
19+
assertThat(
20+
new Solution().minimumAbsDifference(new int[] {1, 3, 6, 10, 15}),
21+
equalTo(ArrayUtils.getLists(new int[][] {{1, 3}})));
22+
}
23+
24+
@Test
25+
void minimumAbsDifference3() {
26+
assertThat(
27+
new Solution().minimumAbsDifference(new int[] {3, 8, -10, 23, 19, -4, -14, 27}),
28+
equalTo(ArrayUtils.getLists(new int[][] {{-14, -10}, {19, 23}, {23, 27}})));
29+
}
30+
}

0 commit comments

Comments
 (0)