Heap of fronts + running max
Time O(N log k) Space O(k)Push the first element of each list and track the max. Repeatedly: the range is heap.min to max; record it if smaller; pop the min and push the next element from its list (updating max). Stop when a list runs out.
import java.util.*;
class Solution {
public int[] smallestRange(List<List<Integer>> nums) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
int max = Integer.MIN_VALUE;
for (int i = 0; i < nums.size(); i++) {
int v = nums.get(i).get(0);
pq.offer(new int[]{v, i, 0});
max = Math.max(max, v);
}
int[] best = {pq.peek()[0], max};
while (true) {
int[] t = pq.poll();
if (max - t[0] < best[1] - best[0]) best = new int[]{t[0], max};
List<Integer> list = nums.get(t[1]);
if (t[2] + 1 == list.size()) return best;
int v = list.get(t[2] + 1);
pq.offer(new int[]{v, t[1], t[2] + 1});
max = Math.max(max, v);
}
}
}Verdict: Each element is pushed once.