Smallest Range Covering Elements from K Lists

IF
AlgoAxiomStaff Engineers
JSTS
Hard20 mins

You have k lists of sorted integers. Find the smallest range [a, b] such that there is at least one integer from each of the k lists.

Among all the ranges, choose the range with the smallest gap b - a. If there are multiple answers, return the one with the smallest a.

Examples

Example 1:

Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]

Output: [20,24]

Explanation: [20,24] covers 24 from list 0, 20 from list 1, and 22 from list 2.

Example 2:

Input: nums = [[1,2,3],[1,2,3],[1,2,3]]

Output: [1,1]

Explanation: Every list contains 1, so [1,1] is valid with gap 0.

Example 3:

Input: nums = [[10],[10],[10]]

Output: [10,10]

Explanation: All lists contain only 10.

Constraints

  • nums.length == k
  • 1 <= k <= 3500
  • 1 <= nums[i].length <= 50
  • -10^5 <= nums[i][j] <= 10^5
  • nums[i] is sorted in ascending order.
Source: K-way Merge pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle