Smallest Range Covering Elements from K Lists
Real-world version: Tightest Gauge BandThe statement here is our own: the same inputs, outputs and technique.
A field survey runs k gauges. Gauge i filed the readings in nums[i],
already arranged from lowest to highest, and every gauge filed at least one
reading.
Calibration needs a single band of values that reaches every gauge. A band is
written [low, high] with low <= high, and it reaches a gauge when at least
one of that gauge's readings sits inside the band, endpoints included.
Return the tightest band that reaches all k gauges. Tightest means the
smallest high - low. When several bands tie on that width, return the one
whose low is smallest.
Examples
Example 1
- Input
- nums = [[2,11,17],[6,13],[-4,9,15]]
- Output
- [9, 13]
The band [9,13] holds 11 from the first gauge, 13 from the second and 9 from the third, so it reaches all three and its width is 4.
Example 2
- Input
- nums = [[1,5],[3,7]]
- Output
- [1, 3]
Three bands reach both gauges at width 2: [1,3], [3,5] and [5,7]. The tie goes to the smallest low, so the answer is [1,3].
Example 3
- Input
- nums = [[8]]
- Output
- [8, 8]
With one gauge and one reading, the band [8,8] already reaches it and has width 0.
Example 4
- Input
- nums = [[-100000,100000],[0]]
- Output
- [-100000, 0]
The second gauge only ever reads 0, so a band must stretch from 0 to one of -100000 or 100000. Both give width 100000, and the smaller low wins.
Constraints
- nums.length == k
- 1 <= k <= 3500
- 1 <= nums[i].length <= 50
- -10^5 <= nums[i][j] <= 10^5
- nums[i] is sorted in non-decreasing order.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def smallest_range(nums: list[list[int]]) -> list[int]:public int[] smallestRange(List<List<Integer>> nums)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Task SchedulerReal-world version: Press Die CooldownMedium
- Longest Harmonious SubsequenceReal-world version: Widest One-Degree SampleEasy
- High FiveReal-world version: Top Five Average per ApprenticeEasy