HardArrayHash TableGreedySliding WindowSortingHeap (Priority Queue)

1 approach, code in every language

Problem description

You are given k lists of integers, each sorted in non-decreasing order. Find the smallest range [a, b] that includes at least one number from each of the k lists.

A range [a, b] is smaller than another range [c, d] if b - a is smaller than d - c, or if the widths are equal and a is smaller than c.

Return the smallest such range as a two-element list.

Example 1

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

Output: [20,24]

24 comes from the first list, 20 from the second, and 22 from the third. The range [20,24] has width 4 and no narrower range covers all three lists.

Example 2

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

Output: [1,1]

The value 1 exists in every list, so the range [1,1] of width 0 already covers all lists.

Constraints

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

After the pattern

Now solve one you haven't seen.

Tightest Gauge Band is our own problem on the technique behind Smallest Range Covering Elements from K Lists: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free