InferenceAI

Enter at least two characters to search.

Start free

Merge Sorted Streams

hardLinked Lists40 min50 XP

Merge k sorted lists into a single sorted list.

Requirements

  • Run in O(Nlog⁡k)O(N \log k) time, where NN is the total number of elements, using a min-heap. Do not just concatenate and sort.

Examples

EXAMPLE WALKTHROUGHthree lists

A guided view of the published example, not a trace of your code.

INPUT[[[1,4,5],[1,3,4],[2,6]]]
TASKmerge_sorted_streams()
EXPECTED OUTPUT[1,1,2,3,4,4,5,6]

Start with the input values shown in this example.

Example 1 of 3

Input: lists = [[1,4,5],[1,3,4],[2,6]]

Output: [1,1,2,3,4,4,5,6]

Explanation: three lists

Input: lists = []

Output: []

Explanation: no lists

Input: lists = [[],[]]

Output: []

Explanation: empty lists

Hint 1

Push the first element of each list onto a heap along with its list index and position.

Topics

DSALinked ListsPython