Skip to content
BytePatterns

Smallest Range K Lists

HardTwo Heaps & K-Way Merge#k-way-merge#sliding-window~45m

Problem

You are given k lists of numbers, each already sorted in ascending order. Find the narrowest range [low, high] that contains at least one value from every list. If two ranges are equally narrow, return the one that starts earlier.

Examples

Input:  lists = [[4, 10, 15, 24, 26], [0, 9, 12, 20], [5, 18, 22, 30]]
Output: [20, 24]
Why:    24 comes from the first list, 20 from the second, 22 from the third
Input:  lists = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Output: [1, 1]
Why:    all three lists share the value 1, so the range collapses to a point
Input:  lists = [[7], [8], [9]]
Output: [7, 9]
Why:    edge case, one option per list and no choice to make

Hints

0 / 3

Stuck on the idea rather than the code? K-Way Merge covers it.