v9.1.1
Menu

Merge K Sorted Lists

hardFREELinked ListBlind 75

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: 11, 11, and 22.
  • The smallest element, 11, 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: 11, 22, 33, 44, 44, 55, and 66, 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

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.