This question evaluates algorithm design and data structure proficiency, focusing on merging multiple sorted sequences and reasoning about time and space complexity for large numbers of arrays and elements.
You are given k sorted arrays of integers:
arrays[0], arrays[1], ..., arrays[k-1]
arrays[i]
is sorted in
non-decreasing
order.
N
.
Your task is to merge these k sorted arrays into a single sorted array (or list) containing all N elements.
You should design an algorithm that is asymptotically optimal in terms of time complexity for large k and N. A straightforward approach that repeatedly scans all array heads at each step is too slow when k is large.
Output: a single sorted sequence of all elements from the k arrays.
Input:
k = 3
arrays[0] = [1, 4, 9]
arrays[1] = [2, 6]
arrays[2] = [0, 3, 7, 8]
Output:
[0, 1, 2, 3, 4, 6, 7, 8, 9]
1 <= k <= 10^5
N
across all arrays:
0 <= N <= 10^6
(assume this upper bound for complexity discussion).
(Hint: Consider a priority-based data structure that efficiently gives you the smallest current element among the k arrays.)