Smallest Range Covering Elements from K Lists

Hard Topics Company Tags

You are given nums, a list of nonempty integer lists, each sorted in nondecreasing order. Return an inclusive interval [a,b] that contains at least one value from every list. Minimize the width b-a; if several intervals have the same minimum width, choose the one with the smallest left endpoint a. The lists may have different lengths, and repeated values are allowed.

Example 1:

Input: nums = [[1,8],[3,9],[4,10]]

Output: [8,10]

Explanation: The interval [8,10] contains 8 from the first list, 9 from the second, and 10 from the third. Its width is 2; covering the three lists near their smaller values needs [1,4], with width 3. No interval of width 1 covers all three lists.

Example 2:

Input: nums = [[1,5],[2,6]]

Output: [1,2]

Explanation: Both [1,2] and [5,6] cover the two lists and have width 1. Choose [1,2] because its left endpoint is smaller.

Example 3:

Input: nums = [[7,7,9]]

Output: [7,7]

Explanation: There is only one list, so any of its values alone gives a width-0 interval. The smallest such value is 7, so return [7,7].

Constraints:

  • 1 <= nums.length <= 3500
  • Each list contains between 1 and 50 integers.
  • Every value is between -100000 and 100000, inclusive.
  • Each list is sorted in nondecreasing order.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.