Merge Sorted Arrays
Problem statement
You are given arrays, a list of integer arrays. Each array is sorted in non-decreasing order, and some arrays may be empty.
Merge them into one array sorted in non-decreasing order and return it. Keep every value, including repeats: a value that appears three times in total appears three times in the answer.
With two arrays this is the classic merge of two sorted arrays. The general case merges any number of them.
Function
mergeSortedArrays(arrays: int[][]) → int[]Examples
Example 1
arrays = [[1,4,4,9],[2,4,10]]return = [1,2,4,4,4,9,10]Two arrays, the base case. Taking the smaller front value each time gives 1, 2, then the three 4s (two from the first array and one from the second), then 9 and 10. Duplicates are kept.
Example 2
arrays = [[-3,0],[],[-5,-3,8]]return = [-5,-3,-3,0,8]The middle array is empty and adds nothing. The other two merge into -5, -3, -3, 0, 8.
Example 3
arrays = [[7]]return = [7]With one array, the answer is that array.
Constraints
1 <= arrays.length <= 5000 <= arrays[i].length <= 2000- The total number of values in all arrays is at most 2000.
-10^9 <= arrays[i][j] <= 10^9- Each
arrays[i]is sorted in non-decreasing order.