< Summary

Information
Class: LeetCode.Algorithms.SmallestRangeCoveringElementsFromKLists.SmallestRangeCoveringElementsFromKListsSortingWithSlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SmallestRangeCoveringElementsFromKLists\SmallestRangeCoveringElementsFromKListsSortingWithSlidingWindow.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 78
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SmallestRange(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SmallestRangeCoveringElementsFromKLists\SmallestRangeCoveringElementsFromKListsSortingWithSlidingWindow.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.SmallestRangeCoveringElementsFromKLists;
 13
 14/// <inheritdoc />
 15public 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)
 324    {
 325        var mergedNums = new List<(int Value, int ListIndex)>();
 26
 2227        for (var i = 0; i < nums.Count; i++)
 828        {
 3229            mergedNums.AddRange(nums[i].Select(num => (num, i)));
 830        }
 31
 2732        var sortedNums = mergedNums.OrderBy(x => x.Value).ToArray();
 33
 334        var countMap = new Dictionary<int, int>();
 335        var min = 0;
 336        var max = 0;
 337        var start = 0;
 338        var matchedLists = 0;
 339        var diff = int.MaxValue;
 40
 5441        for (var end = 0; end < sortedNums.Length; end++)
 2442        {
 2443            countMap.TryAdd(sortedNums[end].ListIndex, 0);
 44
 2445            countMap[sortedNums[end].ListIndex]++;
 46
 2447            if (countMap[sortedNums[end].ListIndex] == 1)
 1948            {
 1949                matchedLists++;
 1950            }
 51
 4152            while (matchedLists == nums.Count)
 1753            {
 1754                var currentMin = sortedNums[start].Value;
 1755                var currentMax = sortedNums[end].Value;
 1756                var currentDiff = currentMax - currentMin;
 57
 1758                if (currentDiff < diff)
 459                {
 460                    min = currentMin;
 461                    max = currentMax;
 462                    diff = currentDiff;
 463                }
 64
 1765                countMap[sortedNums[start].ListIndex]--;
 66
 1767                if (countMap[sortedNums[start].ListIndex] == 0)
 1468                {
 1469                    matchedLists--;
 1470                }
 71
 1772                start++;
 1773            }
 2474        }
 75
 376        return [min, max];
 377    }
 78}