Merge K Sorted Arrays
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write mergeKSorted(arrays): arrays holds 0 to 5,000 arrays of numbers, each sorted in ascending order, and some may be empty. Return one new array holding every number from all of them in ascending order, repeats included. Don't change the input arrays. For example, mergeKSorted([[1, 4, 5], [1, 3, 4], [2, 6]]) is [1, 1, 2, 3, 4, 4, 5, 6]. The checks include 5,000 arrays with 200,000 numbers in all, so looking at the front of every array to pick each next number is too slow: keep the fronts in a min-heap.
Your code must pass
- Merges a few sorted arrays
- Keeps repeated numbers
- Handles empty arrays and no arrays
- Leaves the input arrays unchanged
- Is fast enough for 5,000 arrays