Median Across N Sorted Arrays
Problem statement
Given arrays, a collection of individually sorted integer arrays, return the median of every value across the collection. The rows may have different lengths or be empty, but at least one value exists overall.
Preserve duplicate multiplicities. For an odd total number of values, return the middle value in the combined sorted order. For an even total, return the arithmetic mean of the two middle values.
After a flatten-and-sort baseline, use each row’s sorted order to locate the required ranks without constructing a merged array.
Function
medianOfSortedArrays(arrays: int[][]) → doubleExamples
Example 1
arrays = [[1,7],[2,6],[3]]return = 3.0The combined order is [1,2,3,6,7].
Example 2
arrays = [[1,4],[],[2,8]]return = 3.0The two middle values are 2 and 4.
Example 3
arrays = [[-9,-3],[0],[2,2,8]]return = 1.0The middle pair is 0 and 2.
Constraints
1 <= arrays.length <= 2000.- The total number of values is between
1and10^5. - Every row is sorted in nondecreasing order.
- Values are signed 32-bit integers.