24 · Heaps & Priority Queues

#632 Smallest Range Covering Elements from K Lists

Hardsolve on LeetCode ↗

frequently reported atAmazonGoogle

the problem

In plain words

You get k lists of integers, each sorted in non-decreasing order. Find the smallest range [a, b] that contains at least one number from every list. A range is smaller when b − a is smaller; when two ranges are equally wide, the one with the smaller a wins.

in nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]

out [20,24]

24 from the first list, 20 from the second and 22 from the third all lie in [20, 24].

in nums = [[1,2,3],[1,2,3],[1,2,3]]

out [1,1]

  • k == nums.size(), 1 ≤ k ≤ 3500
  • 1 ≤ nums[i].size() ≤ 50
  • -10⁵ ≤ nums[i][j] ≤ 10⁵
  • Each list is sorted in non-decreasing order

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #23
    2. #76
      Minimum Window Substring
      Hardin Sliding Window
    3. #373