📘
Merge K Sorted Lists
HardLinked List
Given k sorted arrays, merge them into one sorted array.
Input: each line is a comma-separated sorted array. Output: merged array, space-separated.
Example:
Input:
1,4,5 1,3,4 2,6
Output:
1 1 2 3 4 4 5 6
Reasoning:
- The input consists of three sorted arrays:
[1, 4, 5],[1, 3, 4], and[2, 6]. - We initialize the merge process by comparing the smallest unmerged elements from each array: 1, 1, and 2.
- The smallest element, 1, is added to the output, and the next element from its array is considered for merging.
- This process continues, comparing and merging the smallest unmerged elements: 1, 2, 3, 4, 4, 5, and 6, resulting in the final merged array.
- The final output is the merged array with elements separated by spaces:
1 1 2 3 4 4 5 6
Constraints:
- 0 <= k <= 10^4
- 0 <= total elements <= 10^4
- Each list is sorted in ascending order
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.