Smallest Range Covering Elements from K Lists - Problem
Smallest Range Covering Elements from K Lists

Imagine you're a data analyst trying to find the most efficient way to represent multiple datasets with a single range. You have k lists of sorted integers in non-decreasing order, and your goal is to find the smallest possible range [a, b] that includes at least one number from each of the k lists.

The challenge is in the definition of "smallest": Range [a, b] is smaller than range [c, d] if:
โ€ข b - a < d - c (shorter length wins), OR
โ€ข b - a == d - c AND a < c (same length, but starts earlier)

This problem tests your ability to work with multiple sorted sequences simultaneously and find optimal overlapping ranges.

Input & Output

example_1.py โ€” Basic Case
$ Input: [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
โ€บ Output: [20,24]
๐Ÿ’ก Note: The range [20,24] includes 24 from list 1, 20 from list 2, and 22 from list 3. This gives us a range of size 4, which is optimal. Other ranges like [4,5] would have size 1 but don't include elements from all lists.
example_2.py โ€” Single Element Lists
$ Input: [[1],[2],[3]]
โ€บ Output: [1,3]
๐Ÿ’ก Note: When each list has only one element, we must include all of them. The range spans from the minimum (1) to the maximum (3), giving us [1,3] with size 2.
example_3.py โ€” Overlapping Ranges
$ Input: [[1,2,3],[1,2,3],[1,2,3]]
โ€บ Output: [1,1]
๐Ÿ’ก Note: Since all lists contain the same elements, we can pick the same value (1) from each list, giving us the optimal range [1,1] with size 0.

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
๐ŸŽฏ Concert Venue Booking StrategyBand Preferred Dates:Rock Band:4, 10, 15, 24, 26Jazz Band:0, 9, 12, 20Pop Band:5, 18, 22, 30Booking Timeline:045910121518202224Booking Process:Step 1: Initial Window [0, 4, 5]Window: [0, 5] = 5 daysStep 2: Advance Jazz [4, 5, 9]Window: [4, 9] = 5 daysStep 3: Advance Rock [5, 9, 10]Window: [5, 10] = 5 daysContinue...Exploring...Final Step: Best Window FoundOptimal: [20, 24] = 4 daysโœ“ Rock: 24, Jazz: 20, Pop: 22๐ŸŽฏ Key Strategy:Always advance the earliest date to minimize the booking window while ensuring all bands are covered!
Understanding the Visualization
1
Set Up Initial Window
Start with each band's first preferred date. This gives you k dates to work with.
2
Find Current Window
The current booking window spans from the earliest date to the latest date among your k selected dates.
3
Try to Shrink Window
The band with the earliest date is limiting your window. Try their next preferred date to potentially find a better window.
4
Update Best Window
If the new window is smaller, update your best booking window. Continue until no more dates are available.
5
Optimal Booking Found
You've found the shortest possible booking window that satisfies all bands!
Key Takeaway
๐ŸŽฏ Key Insight: The optimal range always has its boundaries at actual elements from the input lists. By using a min-heap to efficiently track the minimum while maintaining the maximum, we can explore all possible ranges in optimal time complexity.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
78.3K Views
High Frequency
~25 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