| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.MinimumAbsoluteDifference; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumAbsoluteDifferenceLookup : IMinimumAbsoluteDifference |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n + R), where R = maxValue - MinValue + 1 |
| | | 19 | | /// Space complexity - O(R), where R = maxValue - MinValue + 1 |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="arr"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public IList<IList<int>> MinimumAbsDifference(int[] arr) |
| | 3 | 24 | | { |
| | 3 | 25 | | var minValue = int.MaxValue; |
| | 3 | 26 | | var maxValue = int.MinValue; |
| | | 27 | | |
| | 40 | 28 | | for (var i = 0; i < arr.Length; i++) |
| | 17 | 29 | | { |
| | 17 | 30 | | var num = arr[i]; |
| | | 31 | | |
| | 17 | 32 | | minValue = Math.Min(minValue, num); |
| | 17 | 33 | | maxValue = Math.Max(maxValue, num); |
| | 17 | 34 | | } |
| | | 35 | | |
| | 3 | 36 | | Span<bool> numsLookup = stackalloc bool[maxValue - minValue + 1]; |
| | | 37 | | |
| | 40 | 38 | | for (var i = 0; i < arr.Length; i++) |
| | 17 | 39 | | { |
| | 17 | 40 | | var num = arr[i]; |
| | | 41 | | |
| | 17 | 42 | | numsLookup[num - minValue] = true; |
| | 17 | 43 | | } |
| | | 44 | | |
| | 3 | 45 | | var minDifference = int.MaxValue; |
| | | 46 | | |
| | 3 | 47 | | var result = new List<IList<int>>(arr.Length); |
| | | 48 | | |
| | 3 | 49 | | var previousValue = minValue; |
| | | 50 | | |
| | 122 | 51 | | for (var i = 1; i < numsLookup.Length; i++) |
| | 58 | 52 | | { |
| | 58 | 53 | | if (!numsLookup[i]) |
| | 44 | 54 | | { |
| | 44 | 55 | | continue; |
| | | 56 | | } |
| | | 57 | | |
| | 14 | 58 | | var currentValue = minValue + i; |
| | 14 | 59 | | var difference = currentValue - previousValue; |
| | | 60 | | |
| | 14 | 61 | | if (difference < minDifference) |
| | 3 | 62 | | { |
| | 3 | 63 | | minDifference = difference; |
| | | 64 | | |
| | 3 | 65 | | result.Clear(); |
| | 3 | 66 | | } |
| | | 67 | | |
| | 14 | 68 | | if (difference == minDifference) |
| | 7 | 69 | | { |
| | 7 | 70 | | result.Add(new[] |
| | 7 | 71 | | { |
| | 7 | 72 | | previousValue, |
| | 7 | 73 | | currentValue |
| | 7 | 74 | | }); |
| | 7 | 75 | | } |
| | | 76 | | |
| | 14 | 77 | | previousValue = currentValue; |
| | 14 | 78 | | } |
| | | 79 | | |
| | 3 | 80 | | return result; |
| | 3 | 81 | | } |
| | | 82 | | } |