public int[] smallestRange(List<List<Integer>> nums) {
PriorityQueue<Element> minHeap = new PriorityQueue<>();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
minHeap.add(new Element(i, 0, nums.get(i).get(0)));
max = Math.max(max, nums.get(i).get(0));
int range = Integer.MAX_VALUE;
int start = -1, end = -1;
while (minHeap.size() == n) {
Element curNum = minHeap.remove();
if (max - curNum.val < range) {
range = max - curNum.val;
if (curNum.index + 1 < nums.get(curNum.row).size()) {
int nextNum = nums.get(curNum.row).get(curNum.index + 1);
minHeap.add(new Element(curNum.row, curNum.index + 1, nextNum));
max = Math.max(max, nextNum);
return new int[] {start, end};
class Element implements Comparable<Element> {
public Element(int row, int index, int val) {
public int compareTo(Element that) {
return this.val - that.val;