| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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.SmallestRangeCoveringElementsFromKLists; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class SmallestRangeCoveringElementsFromKListsSortingWithSlidingWindow : ISmallestRangeCoveringElementsFromKLists |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int[] SmallestRange(IList<IList<int>> nums) |
| 3 | 24 | | { |
| 3 | 25 | | var mergedNums = new List<(int Value, int ListIndex)>(); |
| | 26 | |
|
| 22 | 27 | | for (var i = 0; i < nums.Count; i++) |
| 8 | 28 | | { |
| 32 | 29 | | mergedNums.AddRange(nums[i].Select(num => (num, i))); |
| 8 | 30 | | } |
| | 31 | |
|
| 27 | 32 | | var sortedNums = mergedNums.OrderBy(x => x.Value).ToArray(); |
| | 33 | |
|
| 3 | 34 | | var countMap = new Dictionary<int, int>(); |
| 3 | 35 | | var min = 0; |
| 3 | 36 | | var max = 0; |
| 3 | 37 | | var start = 0; |
| 3 | 38 | | var matchedLists = 0; |
| 3 | 39 | | var diff = int.MaxValue; |
| | 40 | |
|
| 54 | 41 | | for (var end = 0; end < sortedNums.Length; end++) |
| 24 | 42 | | { |
| 24 | 43 | | countMap.TryAdd(sortedNums[end].ListIndex, 0); |
| | 44 | |
|
| 24 | 45 | | countMap[sortedNums[end].ListIndex]++; |
| | 46 | |
|
| 24 | 47 | | if (countMap[sortedNums[end].ListIndex] == 1) |
| 19 | 48 | | { |
| 19 | 49 | | matchedLists++; |
| 19 | 50 | | } |
| | 51 | |
|
| 41 | 52 | | while (matchedLists == nums.Count) |
| 17 | 53 | | { |
| 17 | 54 | | var currentMin = sortedNums[start].Value; |
| 17 | 55 | | var currentMax = sortedNums[end].Value; |
| 17 | 56 | | var currentDiff = currentMax - currentMin; |
| | 57 | |
|
| 17 | 58 | | if (currentDiff < diff) |
| 4 | 59 | | { |
| 4 | 60 | | min = currentMin; |
| 4 | 61 | | max = currentMax; |
| 4 | 62 | | diff = currentDiff; |
| 4 | 63 | | } |
| | 64 | |
|
| 17 | 65 | | countMap[sortedNums[start].ListIndex]--; |
| | 66 | |
|
| 17 | 67 | | if (countMap[sortedNums[start].ListIndex] == 0) |
| 14 | 68 | | { |
| 14 | 69 | | matchedLists--; |
| 14 | 70 | | } |
| | 71 | |
|
| 17 | 72 | | start++; |
| 17 | 73 | | } |
| 24 | 74 | | } |
| | 75 | |
|
| 3 | 76 | | return [min, max]; |
| 3 | 77 | | } |
| | 78 | | } |