Skip to content

Commit a1f7ff6

Browse files
add 1200c
1 parent 13d8c84 commit a1f7ff6

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Your ideas/fixes/algorithms are more than welcome!
3030
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||
3131
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | | | |Easy||
3232
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | |Easy||
33+
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java) | || |Easy||
3334
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java) | | | |Easy||
3435
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy||
3536
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||

src/main/java/com/fishercoder/solutions/_1200.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
35
import java.util.List;
6+
import java.util.TreeMap;
47

58
/**
69
* 1200. Minimum Absolute Difference
@@ -30,8 +33,26 @@
3033
* */
3134
public class _1200 {
3235
public static class Solution1 {
36+
/**
37+
* Time: O(n)
38+
* Space: O(k) where k is the distinct number of differences between two numbers in the given array
39+
* */
3340
public List<List<Integer>> minimumAbsDifference(int[] arr) {
34-
return null;
41+
Arrays.sort(arr);
42+
TreeMap<Integer, List<List<Integer>>> map = new TreeMap<>();
43+
for (int i = 0; i < arr.length - 1; i++) {
44+
int diff = arr[i + 1] - arr[i];
45+
if (map.containsKey(diff)) {
46+
List list = map.get(diff);
47+
list.add(Arrays.asList(arr[i], arr[i + 1]));
48+
map.put(diff, list);
49+
} else {
50+
List list = new ArrayList<>();
51+
list.add(Arrays.asList(arr[i], arr[i + 1]));
52+
map.put(diff, list);
53+
}
54+
}
55+
return map.firstEntry().getValue();
3556
}
3657
}
3758
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1200;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _1200Test {
14+
private static _1200.Solution1 solution1;
15+
private static int[] arr;
16+
private static List<List<Integer>> expected;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _1200.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
arr = new int[]{4, 2, 1, 3};
26+
expected = new ArrayList<>();
27+
expected.add(Arrays.asList(1, 2));
28+
expected.add(Arrays.asList(2, 3));
29+
expected.add(Arrays.asList(3, 4));
30+
assertEquals(expected, solution1.minimumAbsDifference(arr));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)