You are given k sorted integer arrays in arrays. Find the smallest range [a, b] (the one minimizing b - a) that contains at least one number from each of the k arrays.
If multiple ranges share the same span, return the one with the smaller starting value a. Return the result as a length-2 list [a, b].
Function Signature
def minimal_span(arrays: list[list[int]]) -> list[int]:
Rules
-
A range
[a, b]
is inclusive: it contains a number
x
when
a <= x <= b
.
-
Each array is nonempty and sorted in non-decreasing order. Values may repeat within an array and across arrays.
-
Ranges are compared by span
b - a
first. Among ranges with the same span, the one with the smaller
a
wins, so the answer is unique.
Constraints
-
1 <= len(arrays) <= 3500
-
1 <= len(arrays[i]) <= 50
-
-10^5 <= arrays[i][j] <= 10^5
-
Each
arrays[i]
is sorted in non-decreasing order.
Examples
Input: arrays = [[4, 10, 15, 24, 26], [0, 9, 12, 20], [5, 18, 22, 30]]
Output: [20, 24]
The range [20, 24] contains 24 from the first array, 20 from the second and 22 from the third. No range with a span of 3 or less holds a number from all three arrays.
Input: arrays = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Output: [1, 1]
[1, 1] contains 1 from each array, so its span is 0. [2, 2] and [3, 3] also have span 0, and the smaller start wins.
Input: arrays = [[1, 5], [3, 7]]
Output: [1, 3]
The closest pair of numbers from the two arrays differs by 2, and three ranges reach that span: [1, 3], [3, 5] and [5, 7]. The tie rule picks [1, 3].