|
| 1 | +# [Problem 1200: Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We need all pairs [a, b] (a < b) whose difference equals the minimum absolute difference among any two elements in arr. My first thought is sorting: if arr is sorted, the minimum difference must occur between some pair of adjacent elements — because for any non-adjacent pair there is at least one element between them that creates a smaller or equal gap by the triangle inequality. So sort arr, scan adjacent differences to find the global minimum, then collect all adjacent pairs with that difference. Sorting is O(n log n) which is fine for n up to 1e5. Since values are bounded (-1e6..1e6) an alternative counting-sort / bucket approach could yield O(n + range) but not necessary here. |
| 5 | + |
| 6 | +## Refining the problem, round 2 thoughts |
| 7 | +We should consider edge cases: n is at least 2 per constraints, and elements are distinct so we won't see zero differences except if duplicates were allowed (they're not). After sorting, we do a single pass to compute minDiff, then another pass to collect pairs (or do both in one pass by updating when we find a smaller diff and resetting results). Keep memory usage minimal: output size may be up to n-1 pairs in worst case. Time complexity dominated by sort: O(n log n). Space complexity aside from output is O(1) (or O(n) if counting sort or if we consider the sort in-place requiring O(log n) stack for Timsort). |
| 8 | + |
| 9 | +I'll implement a single-pass after sort that both updates minDiff and maintains result: when we find a smaller diff, reset result to just this pair; when equal, append. |
| 10 | + |
| 11 | +## Attempted solution(s) |
| 12 | +```python |
| 13 | +from typing import List |
| 14 | + |
| 15 | +class Solution: |
| 16 | + def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]: |
| 17 | + # Sort the array first |
| 18 | + arr.sort() |
| 19 | + n = len(arr) |
| 20 | + # Initialize minDiff to a large value |
| 21 | + minDiff = float('inf') |
| 22 | + res: List[List[int]] = [] |
| 23 | + # Single pass to find minDiff and build result |
| 24 | + for i in range(1, n): |
| 25 | + diff = arr[i] - arr[i-1] |
| 26 | + if diff < minDiff: |
| 27 | + minDiff = diff |
| 28 | + res = [[arr[i-1], arr[i]]] |
| 29 | + elif diff == minDiff: |
| 30 | + res.append([arr[i-1], arr[i]]) |
| 31 | + return res |
| 32 | +``` |
| 33 | +- Notes: |
| 34 | + - Approach: sort the array and examine only adjacent pairs. When a smaller difference is found, reset the result list; when equal, append the pair. |
| 35 | + - Time complexity: O(n log n) due to sorting (n = len(arr)). The single scan is O(n). |
| 36 | + - Space complexity: O(1) extra space ignoring the output (or O(n) if counting the output). Sorting in Python (Timsort) uses O(n) worst-case auxiliary but typically O(log n) stack; overall this is acceptable for n up to 1e5. |
| 37 | + - This solution is simple, efficient, and handles the constraints directly. |
0 commit comments