394. Merge K Sorted Lists (Optimal — Min Heap)

HardLinked ListHeapMergeLinked List

Given k sorted linked lists, merge them into a single sorted list using a min-heap for optimal O(N log k) time. Each list is given as a sorted array; return the merged list as an array. Input: a JSON array of arrays.

Input: JSON array of sorted arrays.

Output: Array — the merged sorted list.

Examples

Example 1
Input: [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: Optimal k-way merge.
Example 2
Input: [[]]
Output: []
Explanation: Empty.
Example 3
Input: [[1]]
Output: [1]
Explanation: Single list.

Constraints

Asked by

DeloitteMetaOracleAmazonAppleMicrosoft
Solve this problem in the editor →