You have k
lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each of the k
lists.
We define the range [a,b] is smaller than range [c,d] if b-a < d-c
or a < c
if b-a == d-c
.
Example 1:
Input:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] Output: [20,24] Explanation: List 1: [4, 10, 15, 24,26], 24 is in range [20,24]. List 2: [0, 9, 12, 20], 20 is in range [20,24]. List 3: [5, 18, 22, 30], 22 is in range [20,24].
Note:
k
<= 3500value of elements
<= 105.The naive approach is to consider every pair of elements, and from amongst the given \nlists and consider the range formed by these elements. For every range currently considered, we can traverse over all the \nlists to find if atleast one element from these lists can be included in the current range. If so, we store the end-points of the current range \nand compare it with the previous minimum range found, if any, satisfying the required criteria, to find the smaller range from among them.
\nOnce all the element pairs have been considered as the ranges, we can obtain the required minimum range.
\n\nComplexity Analysis
\nTime complexity : . Considering every possible range(element pair) requires time. For each range considered, \nwe need to traverse over all the elements of the given lists in the worst case requiring another time. Here, refers to the \ntotal number of elements in the given lists.
\nSpace complexity : . Constant extra space is used.
\nAlgorithm
\nIn the last approach, we consider every possible range and then traverse over every list to check if atleast one of the \nelements from these lists lies in the required range. Instead of doing this traversal for every range, we can make use \nof Binary Search to find the index of the element just larger than(or equal to) the lower limit of the range currently \nconsidered.
\nIf all the elements in the current list are lesser than this lower limit, we\'ll get the index as \n for the list being currently checked. In this case, none of the elements of the current list lies in the\ncurrent range.
\nOn the other hand, if all the elements in this list are larger than this lower limit, we\'ll get the index of the first element(minimum) in the current list. If this element happens to be larger than the upper limit of the range currently considered, then also, none of the elements of the current list lies within the current range.
\nWhenever a range is found which satisfies the required criteria, we can compare it with the minimum range found so far \n to determine the required minimum range.
\n\nComplexity Analysis
\nTime complexity : . The time required to consider every possible range is . For every range currently considered, \na Binary Search requiring time is required. Here, refers to the total number of elements in the given \nlists and refers to the average length of each list.
\nSpace complexity : . Constant extra space is used.
\nAlgorithm
\nWe\'ll discuss about the implementation used in the current approach along with the idea behind it.
\nThis approach makes use of an array of pointers, , whose length is equal to the number of given lists. In this \narray, refers to the element which needs to be considered next in the list. The meaning of this will become \nmore clearer when we look at the process.
\nWe start by initializing all the elements of to 0. Thus, currently, we are considering the first(minimum) element \namong all the lists. Now, we find out the index of the list containing the maximum() and minimum() \nelements from amongst the elements currently pointed by . The range formed by these maximum and minimum elements surely
\ncontains atleast one element from each list.
But, now our objective is to minimize this range. To do so, there are two options: Either decrease the maximum value or increase the \nminimum value.
\nNow, the maximum value can\'t be reduced any further, since it already corresponds to the minimum value in one of the lists. \nReducing it any further will lead to the exclusion of all the elements of this list(containing the last maximum value) \nfrom the new range.
\nThus, the only option left in our hand is to try to increase the minimum value. To do so, we now need to consider the\n next element in the list containing the last minimum value. Thus, we increment the entry at the corresponding index\n in , to indicate that the next element in this list now needs to be considered.
\nThus, at every step, we find the maximum and minimum values being pointed currently, update the values \n appropriately, and also find out the range formed by these maximum and minimum values to find out the smallest range \n satisfying the given criteria.
\nWhile doing this process, if any of the lists gets completely exhausted, it means that the minimum value being increased for \n minimizing the range being considered can\'t be increased any further, without causing the exclusion of all the elements in atleast \n one of the lists. Thus, we can stop the search process whenever any list gets completely exhausted.
\nWe can also stop the process, when all the elements of the given lists have been exhausted.
\nSummarizing the statements above, the process becomes:
\nInitialize array(pointers) with all 0\'s.
\nFind the indices of the lists containing the minimum() and the maximum() elements amongst the elements pointed by the array.
\nIf the range formed by the maximum and minimum elements found above is larger than the previous maximum range, update the boundary values used for the maximum range.
\nIncrement the pointer .
\nRepeat steps 2 to 4 till any of the lists gets exhausted.
\nThe animation below illustrates the process for a visual understanding of the process.
\n!?!../Documents/632_Smallest_Range.json:1000,563!?!
\n\nComplexity Analysis
\nTime complexity : . In the worst case, we need to traverse over array(of length ) for all the elements of the given lists.\nHere, refers to the total number of elements in all the lists. refers to the total number of lists.
\nSpace complexity : . array of size is used.
\nAlgorithm
\nIn the last approach, at each step, we update the pointer corresponding to the current minimum element and traverse over the whole\n array to determine the new maximum and minimum values. We can do some optimization here, by making use of a simple observation.
\nWhenever we update a single entry of to consider the new maximum and minimum values(if we already know the last maximum \nand minimum values), all the elements to be considered for finding the maximum and minimum values remain the same except the new element \nbeing pointed by a single updated entry in . This new entry is certainly larger than the last minimum value(since that was the \nreasoning behind the updation).
\nThus, we can\'t be sure whether this is the new minimum element or not. But, since it is larger than the last \nvalue being considered, it could be a potential competitor for the new maximum value. Thus, we can directly compare it with the last \nmaximum value to determine the current maximum value.
\nNow, we\'re left with finding the minimum value iteratively at every step. To avoid this iterative process, a better idea \nis to make use of a Min-Heap, which stores the values being pointed currently by the array. Thus, the minimum value always \nlies at the top of this heap, and we need not do the iterative search process.
\nAt every step, we remove the minimum element from this heap and find out the range formed by the current maximum and minimum values, and \ncompare it with the minimum range found so far to determine the required minimum range. We also update the increment the index in \ncorresponding to the list containing this minimum entry and add this element to the heap as well.
\nThe rest of the process remains the same as the last approach.
\n\nComplexity Analysis
\nTime complexity : . Heapification of elements requires time. This step could be done \nfor all the elements of the given lists in the worst case. Here, refers to the total number of elements in \nall the lists. refers to the total number of lists.
\nSpace complexity : . array of size is used. A Min-Heap with elements is also used.
\nAnalysis written by: @vinod23
\n