Smallest Range Covering Elements from K Lists - Problem

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.

We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c.

Return the smallest range as an array [start, end].

Input & Output

Example 1 — Basic Case
$ Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
Output: [20,24]
💡 Note: The range [20,24] covers list 1 (24), list 2 (20), and list 3 (22). This gives the minimum range of 4.
Example 2 — Minimum Size
$ Input: nums = [[1,2,3],[1,2,3],[1,2,3]]
Output: [1,1]
💡 Note: All lists contain 1, so the optimal range is [1,1] with range 0.
Example 3 — Different Ranges
$ Input: nums = [[10,20],[15,25],[5,30]]
Output: [15,20]
💡 Note: Range [15,20] covers 20 from list 1, 15 from list 2, and no direct element from list 3, but we need at least one from each. Actually [15,20] covers list 2 (15) and list 1 (20), but we need list 3. The correct answer would be [15,25] covering 20 from list 1, 15 from list 2, and 25 from list 3.

Constraints

  • nums.length == k
  • 1 ≤ k ≤ 3500
  • 1 ≤ nums[i].length ≤ 50
  • -105 ≤ nums[i][j] ≤ 105
  • nums[i] is sorted in non-decreasing order

Visualization

Tap to expand
Smallest Range Covering K ListsInput Lists:List 1: [4, 10, 15, 24, 26]List 2: [0, 9, 12, 20]List 3: [5, 18, 22, 30]Find Minimum Range:Range [20, 24] covers all lists:24 from List 120 from List 222 from List 3Range: [20, 24]Length: 4Goal: Find the smallest possible range that includesat least one number from each of the k listsOutput: [20, 24]Minimum range found
Understanding the Visualization
1
Input
K sorted lists with different ranges of values
2
Process
Find range [a,b] that covers all lists with minimum span
3
Output
Return the smallest range as [start, end]
Key Takeaway
🎯 Key Insight: Use a priority queue to efficiently track the minimum element from each list while maintaining the maximum, creating a sliding window approach that finds the optimal range in O(n log k) time.
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
89.2K Views
Medium Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen