> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Smallest Range Covering Elements from K Lists

> Tested Python solution for LeetCode 632 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 632, Hard. Topics: Array, Greedy, Heap, Sliding Window. [View on LeetCode](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 632   # by problem number
lcpy gen -s smallest_range_covering_elements_from_k_lists   # by problem name
```

## 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`.

### Examples

```
Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
Output: [20,24]
Explanation:
List 1: [4, 10, 15, 24, 26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].
```

```
Input: nums = [[1,2,3],[1,2,3],[1,2,3]]
Output: [1,1]
```

### 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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/smallest_range_covering_elements_from_k_lists/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/smallest_range_covering_elements_from_k_lists/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import heapq


class Solution:
    # Min-heap holds current element from each list. Range = [heap_min, current_max].
    # Repeatedly pop min and advance that list; track smallest range seen.
    # Time: O(n log k) where n = total elements, k = number of lists
    # Space: O(k)
    def smallest_range(self, nums: list[list[int]]) -> list[int]:
        heap: list[tuple[int, int, int]] = []
        current_max = -(10**5) - 1
        for list_idx, lst in enumerate(nums):
            val = lst[0]
            heapq.heappush(heap, (val, list_idx, 0))
            current_max = max(current_max, val)

        best_start, best_end = -(10**5) - 1, 10**5 + 1

        while heap:
            min_val, list_idx, elem_idx = heapq.heappop(heap)
            # Candidate range covers all lists: [min_val, current_max]
            if current_max - min_val < best_end - best_start:
                best_start, best_end = min_val, current_max
            # Advance the list that supplied the min; stop if exhausted
            if elem_idx + 1 == len(nums[list_idx]):
                break
            next_val = nums[list_idx][elem_idx + 1]
            current_max = max(current_max, next_val)
            heapq.heappush(heap, (next_val, list_idx, elem_idx + 1))

        return [best_start, best_end]
```

## Complexity

| Time                                                     | Space |
| -------------------------------------------------------- | ----- |
| O(n log k) where n = total elements, k = number of lists | O(k)  |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
