HardArrayHash TableGreedySliding WindowSortingHeap (Priority Queue)

Smallest Range Covering Elements from K Lists

LeetCode
1 approach, code in all languages

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
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now