Similar Problems

Similar Problems not available

Smallest Range I - Leetcode Solution

Companies:

LeetCode:  Smallest Range I Leetcode Solution

Difficulty: Easy

Topics: math array  

Problem Statement:

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:

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], inclusive range: [4, 26] List 2: [0, 9, 12, 20], inclusive range: [0, 20] List 3: [5, 18, 22, 30], inclusive range: [5, 30] The overall range is [20, 24] since it includes at least one number from all the 3 lists.

Solution:

The problem requires finding the smallest possible range that includes at least one number from each list. We can do this by iterating through each list and keeping track of the minimum and maximum numbers we have seen so far.

We start by initializing a two-dimensional array minMax, where the first index is the list number (0 to k-1) and the second index is either 0 or 1, indicating whether we have seen the minimum or the maximum value for that list. We initialize this array as follows:

minMax = [[list[0][0], list[0][len(list[0]) - 1]] for list in lists]

We then create a min heap containing the first element of each list, along with the index of the list it belongs to. We also keep track of the maximum element we have seen so far in this heap, which starts as minus infinity. We will use this later to calculate the smallest possible range.

heap = [(list[0], i) for i, list in enumerate(lists)] heapq.heapify(heap) maxHeap = max(list[0] for list in lists)

We then start iterating through the heap, popping the smallest element and adding the next element from the list it belongs to. We use a sliding window approach using a two pointer technique where we move the left pointer and compare the maximum elements to get the smallest range. At each iteration, we calculate the current range and update the smallest range accordingly. The following code implements this approach:

left = 0 rangeStart, rangeEnd = -sys.maxsize, sys.maxsize

while len(heap) == len(lists): num, i = heapq.heappop(heap) if minMax[i][0] == num: if i == len(lists) - 1: break heapq.heappush(heap, (lists[i][1], i+1)) minMax[i][0] = lists[i][1] else: if i == len(lists) - 1: break heapq.heappush(heap, (num, i))

maxHeap = max(maxHeap, lists[i][lists[i].index(minMax[i][0])+1])
if maxHeap - num < rangeEnd - rangeStart:
    rangeStart, rangeEnd = num, maxHeap

return [rangeStart, rangeEnd]

The overall time complexity of this solution is O(N log k), where N is the total number of elements in all the lists and k is the number of lists. This is because we insert and remove elements from the heap k*N times in total. The space complexity is O(k), which is the space required to store the minimum and maximum values for each list.

Smallest Range I Solution Code

1