Maintain a queue that have exact one element from each array.

The range is difference of max and min value in the queue in different phase.

Compare all the ranges to find the minimum range.

class Solution {
public int[] smallestRange(List<List<Integer>> nums) {
PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a,b)->{
return a[0] - b[0];
});
int max = Integer.MIN_VALUE, range = Integer.MAX_VALUE;
for(int i = 0; i < nums.size(); i++){
List<Integer> list = nums.get(i);
pq.offer(new int[]{list.get(0), i, 0});
max = Math.max(max, list.get(0));
}
int start = 0, end = max;
while(pq.size() == nums.size()){
int[] node = pq.poll();
if(max - node[0] < range){
start = node[0];
end = max;
range = end - start;
}
List<Integer> list = nums.get(node[1]);
if(node[2] + 1 < list.size()){
int[] newNode = {list.get(node[2] + 1), node[1], node[2] + 1};
pq.offer(newNode);
max = Math.max(max, newNode[0]);
}
}
return new int[]{start, end};
}
}