Skip to content

Commit f21a066

Browse files
solves minimum absolute difference
1 parent 6d40e1f commit f21a066

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@
315315
| 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops) | [![Java](assets/java.png)](src/DistanceBetweenBusStops.java) | |
316316
| 1185 | [Day of the Week](https://leetcode.com/problems/day-of-the-week) | [![Java](assets/java.png)](src/DayOfWeek.java) | |
317317
| 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons) | [![Java](assets/java.png)](src/MaximumNumberOfBalloons.java) | |
318-
| 1196 | [How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket) | | |
319-
| 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference) | | |
318+
| 1196 | 🔒 [How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket) | | |
319+
| 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference) | [![Java](assets/java.png)](src/MinimumAbsoluteDifference.java) | |
320320
| 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences) | | |
321321
| 1213 | [Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays) | | |
322322
| 1217 | [Play With Chips](https://leetcode.com/problems/play-with-chips) | | |

src/MinimumAbsoluteDifference.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
public class MinimumAbsoluteDifference {
6+
public List<List<Integer>> minimumAbsDifference(int[] array) {
7+
List<List<Integer>> result = new ArrayList<>();
8+
Arrays.sort(array);
9+
long minAbsDifference = Long.MAX_VALUE;
10+
for (int i = 0 ; i < array.length - 1 ; i++) {
11+
if (array[i + 1] - array[i] < minAbsDifference) {
12+
minAbsDifference = array[i + 1] - array[i];
13+
result = new ArrayList<>();
14+
result.add(List.of(array[i], array[i + 1]));
15+
} else if (array[i + 1] - array[i] == minAbsDifference) {
16+
result.add(List.of(array[i], array[i + 1]));
17+
}
18+
}
19+
return result;
20+
}
21+
}

0 commit comments

Comments
 (0)