Smallest Range Covering Elements from K Lists

You have k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists.

A range [a, b] is smaller than a range [c, d] if b - a < d - c, or if a < c when b - a == d - c.

Return the smallest range as an array [a, b].

Example 1
Inputnums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
Output[20,24]
List 1 contains 24, list 2 contains 20, and list 3 contains 22, so each list has at least one number in the range [20, 24].
Example 2
Inputnums = [[1,2,3],[1,2,3],[1,2,3]]
Output[1,1]
All three lists contain 1, so the smallest range covering every list is [1, 1].

Constraints

  • nums.length == k
  • 1 <= k <= 3500
  • 1 <= nums[i].length <= 50
  • -10^5 <= nums[i][j] <= 10^5
  • nums[i] is sorted in non-decreasing order.

Asked at 9 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate