Ctrl K

Narrowest Range Touching Every List

Hard
You get k integer lists lists, each sorted in non-decreasing order. Find a range [a, b] that contains at least one number from every list and return it as [a, b]. Range [a, b] beats range [c, d] if b − a < d − c, or if the lengths are equal and a < c. Return the best qualifying range.
Example 1
lists=[[1, 8, 20], [4, 9], [7, 30]] → [7, 9]

The range [7, 9] contains 8 from the first list, 9 from the second and 7 from the third. No shorter range works.

Example 2
lists=[[2, 6], [3, 5]] → [2, 3]

Both [2, 3] and [5, 6] work and have length 1. On a tie, the one that starts earlier wins.

Example 3
lists=[[5], [5, 5], [-1, 5]] → [5, 5]
Constraints
  • 1 ≤ k = len(lists) ≤ 3500
  • 1 ≤ len(lists[i]) ≤ 50
  • -10⁵ ≤ lists[i][j] ≤ 10⁵
  • Each lists[i] is sorted in non-decreasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShowcase: Profit Plus Variety HardJobs With a Cooldown MediumSpread Out the Labels MediumCross Out Half the List MediumClearing the Stacks Evenly Easy
loading Python…
Input
lists=[[1, 8, 20], [4, 9], [7, 30]]
Expected
[7, 9]

Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.