You have k lists of sorted integers in non-decreasing&nbsp;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 &lt; d - c or a &lt; c if b - a == d - c.

&nbsp;
Example 1:

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


Example 2:

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


&nbsp;
Constraints:


	nums.length == k
	1 &lt;= k &lt;= 3500
	1 &lt;= nums[i].length &lt;= 50
	-105 &lt;= nums[i][j] &lt;= 105
	nums[i]&nbsp;is sorted in non-decreasing order.


from queue import PriorityQueue
class Solution:
	def smallestRange(self, nums: List[List[int]]) -> List[int]:
		q = PriorityQueue()
		maxi = -10**7
		mini = 10**7
		for i in range(len(nums)):
			maxi = max(maxi,nums[i][0])            
			mini = min(mini,nums[i][0])
			q.put((nums[i][0],i,0))  

		s, e = mini, maxi
		while not q.empty() :
			mini, i, j = q.get()
			if maxi - mini < e-s:
				s,e = mini, maxi
			if j+1 < len(nums[i]):
				maxi = max(maxi,nums[i][j+1])
				q.put((nums[i][j+1],i,j+1))
			else: break
		return [s,e]
class Solution {
    
    class Pair implements Comparable<Pair> {
        int val;
        int li;
        int di;
        
        public Pair(int val, int li, int di) {
            this.val = val;
            this.li = li;
            this.di = di;
        }
        
        public int compareTo(Pair other) {
            return this.val - other.val;
        }
    }
    
    public int[] smallestRange(List<List<Integer>> nums) {
        PriorityQueue<Pair> pq = new PriorityQueue<>();
        int max = Integer.MIN_VALUE;
        for(int i=0; i<nums.size(); i++) {
            pq.add(new Pair(nums.get(i).get(0), i, 0));
            max = Math.max(max, nums.get(i).get(0));
        }
        
        int rbeg = 0;
        int rend = 0;
        int rsize = Integer.MAX_VALUE;
        
        while(pq.size() == nums.size()) {
            Pair rem = pq.remove();
            int crsize = max - rem.val;
            
            if(crsize < rsize) {
                rsize = crsize;
                rbeg = rem.val;
                rend = max;
            }
            
            if(rem.di < nums.get(rem.li).size() - 1) {
                pq.add(new Pair(nums.get(rem.li).get(rem.di + 1), rem.li, rem.di + 1));
                max = Math.max(max, nums.get(rem.li).get(rem.di + 1));
            }
        }
        
        return new int[]{rbeg, rend};
    }
}
#include <vector>
#include <queue>
#include <limits>

using namespace std;

struct Item {
    int val;
    int r;
    int c;
    
    Item(int val, int r, int c): val(val), r(r), c(c) {
    }
};

struct Comp {
    bool operator() (const Item& it1, const Item& it2) {
        return it2.val < it1.val;
    }
};

class Solution {
public:
    vector<int> smallestRange(vector<vector<int>>& nums) {
        priority_queue<Item, vector<Item>, Comp> pq;
        
        int high = numeric_limits<int>::min();
        int n = nums.size();
        for (int i = 0; i < n; ++i) {
            pq.push(Item(nums[i][0], i, 0));
            high = max(high , nums[i][0]);
        }
        int low = pq.top().val;
        
        vector<int> res{low, high};
        
        while (pq.size() == (size_t)n) {
            auto it = pq.top();
            pq.pop();
            
            if ((size_t)it.c + 1 < nums[it.r].size()) {
                pq.push(Item(nums[it.r][it.c + 1], it.r, it.c + 1));
                high = max(high, nums[it.r][it.c + 1]);
                low = pq.top().val;
                if (high - low < res[1] - res[0]) {
                    res[0] = low;
                    res[1] = high;
                }
            }
        }
        
        return res;
    }
};
var smallestRange = function(nums) {
    let minHeap = new MinPriorityQueue({
        compare: (a,b) => a[0] - b[0]
    });
    let start = 0, end = Infinity;
    let maxSoFar = -Infinity;
	
    for (let num of nums) {
        minHeap.enqueue([num[0], 0, num]);
        maxSoFar = Math.max(maxSoFar, num[0]);
    }
	
    while (minHeap.size() == nums.length) {
        let [num, i, list] = minHeap.dequeue();
        
        if (end - start > maxSoFar - num) {
            start = num;
            end = maxSoFar;
        }
        
        if (list.length > i + 1) {
            minHeap.enqueue([list[i + 1], i + 1, list]);
            maxSoFar = Math.max(maxSoFar, list[i + 1]);
        }
    }
    
    return [start, end];
};
Smallest Range Covering Elements from K Lists