Merge Several Sorted Arrays
Problem statement
You are given a list of integer arrays, arrays. Each array is already sorted from smallest to largest, and some arrays may be empty.
Return one array that holds every value from every input array, sorted from smallest to largest. Keep repeated values: if a value appears three times in total, it appears three times in the answer.
Function
mergeSortedArrays(arrays: int[][]) → int[]Examples
Example 1
arrays = [[1,4,9],[2,3,10],[5]]return = [1,2,3,4,5,9,10]Taking the smallest front value each time gives 1, 2, 3, 4, 5, 9, 10.
Example 2
arrays = [[-3,0,0],[],[0,7]]return = [-3,0,0,0,7]The empty array adds nothing. The three zeros all stay: [-3, 0, 0, 0, 7].
Example 3
arrays = [[6,8]]return = [6,8]With one array there is nothing to merge, so it comes back unchanged.
Constraints
1 <= arrays.length <= 10000 <= arrays[i].length, and all arrays together hold at most2000values-10^5 <= arrays[i][j] <= 10^5- Each
arrays[i]is sorted in non-decreasing order.