InferenceAI

Enter at least two characters to search.

Start free

Merge K Sorted Lists

hardHeaps40 min50 XP

Merge k sorted lists into a single sorted list.

Requirements

  • O(Nlog⁡k)O(N \log k) with a min-heap holding the current head of each list.

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_k_sorted_lists()
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: one empty list

Hint 1

Push (value, list index, element index) for each list's first element; pop the smallest and push its successor.

Topics

DSAHeapsPython