from heapq import * def k_smallest_number(lists, k): # storing the length of lists to use it in a loop later list_length = len(lists) # declaring a min-heap to keep track of smallest elements kth_smallest = [] for index in range(list_length): # if there are no elements in the input list, then continue to the next iteration if len(lists[index]) == 0: continue else: # placing the first element of each list in the min-heap heappush(kth_smallest, (lists[index][0], index, 0)) # set a counter to match if our kth element # equals to that counter, return that number numbers_checked, smallest_number = 0, 0 while kth_smallest: # iterating over the elements pushed in our min-heap # get the smallest number from top of heap and its corresponding list and index smallest_number, list_index, num_index = heappop(kth_smallest) numbers_checked += 1 if numbers_checked == k: break # if there are more elements in list of the top element, # add the next element of that list to the min-heap if num_index + 1 < len(lists[list_index]): heappush( kth_smallest, (lists[list_index][num_index + 1], list_index, num_index + 1)) # return the Kth number found in input lists return smallest_number # driver code def main(): # multiple inputs for efficient results lists = [[[2, 6, 8], [3,6,10], [5, 8, 11]], [[1, 2, 3], [4, 5], [6, 7, 8, 15], [10, 11, 12, 13], [5, 10]], [[], [], []], [[1, 1, 3, 8], [5, 5, 7, 9], [3, 5, 8, 12]], [[5, 8, 9, 17], [], [8, 17, 23, 24]]] k = [5, 50, 7, 4, 8] # loop to execute till the length of list k for i in range(len(k)): print(i + 1, ".\t Input lists: ", lists[i], f"\n\t K = {k[i]}", f"\n\t {k[i]}th smallest number from the given lists is: ", k_smallest_number(lists[i], k[i]), sep="") print("-" * 100) if __name__ == '__main__': main()