vector<int> smallestRange(vector<vector<int>>& nums) {
long long bestLo = INT_MIN, bestHi = INT_MAX;
for (auto& list : nums) {
for (int lo : list) {
long long hi = lo;
bool ok = true;
for (auto& other : nums) {
auto it = lower_bound(other.begin(), other.end(), lo);
if (it == other.end()) { ok = false; break; }
hi = max(hi, (long long)*it);
}
if (ok && (hi - lo < bestHi - bestLo || (hi - lo == bestHi - bestLo && lo < bestLo))) { bestLo = lo; bestHi = hi; }
}
}
return {(int)bestLo, (int)bestHi};
}
public int[] smallestRange(List<List<Integer>> nums) {
long bestLo = Integer.MIN_VALUE, bestHi = Integer.MAX_VALUE;
for (List<Integer> list : nums) {
for (int lo : list) {
long hi = lo;
boolean ok = true;
for (List<Integer> other : nums) {
Integer pick = null;
for (int v : other) if (v >= lo) { pick = v; break; }
if (pick == null) { ok = false; break; }
hi = Math.max(hi, pick);
}
if (ok && (hi - lo < bestHi - bestLo || (hi - lo == bestHi - bestLo && lo < bestLo))) { bestLo = lo; bestHi = hi; }
}
}
return new int[]{(int) bestLo, (int) bestHi};
}
def smallest_range(nums):
best_lo, best_hi = float('-inf'), float('inf')
for lst in nums:
for lo in lst:
hi = lo
ok = True
for other in nums:
pick = next((v for v in other if v >= lo), None)
if pick is None:
ok = False; break
hi = max(hi, pick)
if ok and (hi - lo < best_hi - best_lo or (hi - lo == best_hi - best_lo and lo < best_lo)): best_lo, best_hi = lo, hi
return [best_lo, best_hi]
function smallestRange(nums: number[][]): number[] {
let best = [-Infinity, Infinity];
for (const list of nums) {
for (const lo of list) {
let hi = lo;
let ok = true;
for (const other of nums) {
const pick = other.find((v) => v >= lo);
if (pick === undefined) { ok = false; break; }
hi = Math.max(hi, pick);
}
if (ok && (hi - lo < best[1] - best[0] || (hi - lo === best[1] - best[0] && lo < best[0]))) best = [lo, hi];
}
}
return best;
}
vector<int> smallestRange(vector<vector<int>>& nums) {
vector<pair<int, int>> items;
for (int id = 0; id < (int)nums.size(); id++) for (int v : nums[id]) items.push_back({v, id});
sort(items.begin(), items.end());
int need = nums.size();
vector<int> count(need, 0);
int covered = 0, left = 0;
long long bestLo = INT_MIN, bestHi = INT_MAX;
for (int right = 0; right < (int)items.size(); right++) {
if (count[items[right].second]++ == 0) covered++;
while (covered == need) {
if (items[right].first - items[left].first < bestHi - bestLo) { bestLo = items[left].first; bestHi = items[right].first; }
if (--count[items[left].second] == 0) covered--;
left++;
}
}
return {(int)bestLo, (int)bestHi};
}
public int[] smallestRange(List<List<Integer>> nums) {
List<int[]> items = new ArrayList<>();
for (int id = 0; id < nums.size(); id++) for (int v : nums.get(id)) items.add(new int[]{v, id});
items.sort((a, b) -> Integer.compare(a[0], b[0]));
int need = nums.size();
int[] count = new int[need];
int covered = 0, left = 0;
long bestLo = Integer.MIN_VALUE, bestHi = Integer.MAX_VALUE;
for (int right = 0; right < items.size(); right++) {
if (count[items.get(right)[1]]++ == 0) covered++;
while (covered == need) {
if (items.get(right)[0] - items.get(left)[0] < bestHi - bestLo) { bestLo = items.get(left)[0]; bestHi = items.get(right)[0]; }
if (--count[items.get(left)[1]] == 0) covered--;
left++;
}
}
return new int[]{(int) bestLo, (int) bestHi};
}
def smallest_range(nums):
items = sorted((v, i) for i, lst in enumerate(nums) for v in lst)
need = len(nums)
count = [0] * need
covered = left = 0
best_lo, best_hi = float('-inf'), float('inf')
for right in range(len(items)):
count[items[right][1]] += 1
if count[items[right][1]] == 1: covered += 1
while covered == need:
if items[right][0] - items[left][0] < best_hi - best_lo: best_lo, best_hi = items[left][0], items[right][0]
count[items[left][1]] -= 1
if count[items[left][1]] == 0: covered -= 1
left += 1
return [best_lo, best_hi]
function smallestRange(nums: number[][]): number[] {
const items: [number, number][] = [];
nums.forEach((list, id) => list.forEach((v) => items.push([v, id])));
items.sort((a, b) => a[0] - b[0]);
const need = nums.length;
const count = new Array<number>(need).fill(0);
let covered = 0, left = 0;
let best = [-Infinity, Infinity];
for (let right = 0; right < items.length; right++) {
if (count[items[right][1]]++ === 0) covered++;
while (covered === need) {
if (items[right][0] - items[left][0] < best[1] - best[0]) best = [items[left][0], items[right][0]];
if (--count[items[left][1]] === 0) covered--;
left++;
}
}
return best;
}
vector<int> smallestRange(vector<vector<int>>& nums) {
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> heap; // {value, list, index}
long long mx = INT_MIN;
for (int id = 0; id < (int)nums.size(); id++) {
heap.push({nums[id][0], id, 0});
mx = max(mx, (long long)nums[id][0]);
}
long long bestLo = INT_MIN, bestHi = INT_MAX;
for (;;) {
auto [value, id, idx] = heap.top(); heap.pop();
if (mx - value < bestHi - bestLo) { bestLo = value; bestHi = mx; }
if (idx + 1 == (int)nums[id].size()) break;
int next = nums[id][idx + 1];
heap.push({next, id, idx + 1});
mx = max(mx, (long long)next);
}
return {(int)bestLo, (int)bestHi};
}
public int[] smallestRange(List<List<Integer>> nums) {
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0])); // {value, list, index}
long max = Integer.MIN_VALUE;
for (int id = 0; id < nums.size(); id++) {
heap.add(new int[]{nums.get(id).get(0), id, 0});
max = Math.max(max, nums.get(id).get(0));
}
long bestLo = Integer.MIN_VALUE, bestHi = Integer.MAX_VALUE;
while (true) {
int[] top = heap.poll();
int value = top[0], id = top[1], idx = top[2];
if (max - value < bestHi - bestLo) { bestLo = value; bestHi = max; }
if (idx + 1 == nums.get(id).size()) break;
int next = nums.get(id).get(idx + 1);
heap.add(new int[]{next, id, idx + 1});
max = Math.max(max, next);
}
return new int[]{(int) bestLo, (int) bestHi};
}
def smallest_range(nums):
heap = [] # (value, list, index)
mx = float('-inf')
for i, lst in enumerate(nums):
heapq.heappush(heap, (lst[0], i, 0))
mx = max(mx, lst[0])
best_lo, best_hi = float('-inf'), float('inf')
while True:
value, i, idx = heapq.heappop(heap)
if mx - value < best_hi - best_lo: best_lo, best_hi = value, mx
if idx + 1 == len(nums[i]): break
nxt = nums[i][idx + 1]
heapq.heappush(heap, (nxt, i, idx + 1))
mx = max(mx, nxt)
return [best_lo, best_hi]
function smallestRange(nums: number[][]): number[] {
const heap = new Heap<number[]>((a, b) => a[0] < b[0]);
let max = -Infinity;
nums.forEach((list, id) => {
heap.push([list[0], id, 0]);
max = Math.max(max, list[0]);
});
let best = [-Infinity, Infinity];
for (;;) {
const [value, id, idx] = heap.pop();
if (max - value < best[1] - best[0]) best = [value, max];
if (idx + 1 === nums[id].length) break;
const next = nums[id][idx + 1];
heap.push([next, id, idx + 1]);
max = Math.max(max, next);
}
return best;
}
class Heap<T> {
private a: T[] = [];
constructor(private before: (x: T, y: T) => boolean) {}
get size() { return this.a.length; }
peek(): T { return this.a[0]; }
push(v: T) {
const a = this.a;
a.push(v);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.before(a[i], a[p])) break;
[a[i], a[p]] = [a[p], a[i]];
i = p;
}
}
pop(): T {
const a = this.a;
const top = a[0];
const last = a.pop()!;
if (a.length) {
a[0] = last;
let i = 0;
for (;;) {
let m = i;
const l = 2 * i + 1, r = l + 1;
if (l < a.length && this.before(a[l], a[m])) m = l;
if (r < a.length && this.before(a[r], a[m])) m = r;
if (m === i) break;
[a[i], a[m]] = [a[m], a[i]];
i = m;
}
}
return top;
}
}
vector<int> smallestRange(vector<vector<int>>& nums) {
int k = nums.size();
if (k == 0) return {};
for (auto& list : nums) if (list.empty()) return {};
vector<int> pos(k, 0);
auto later = [&](int a, int b) {
int va = nums[a][pos[a]], vb = nums[b][pos[b]];
return va != vb ? va > vb : a > b;
};
priority_queue<int, vector<int>, decltype(later)> heap(later);
long long mx = INT_MIN;
for (int id = 0; id < k; id++) {
heap.push(id);
mx = max(mx, (long long)nums[id][0]);
}
long long bestLo = nums[heap.top()][0], bestHi = mx;
for (;;) {
int id = heap.top(); heap.pop();
long long value = nums[id][pos[id]];
if (mx - value < bestHi - bestLo) { bestLo = value; bestHi = mx; }
if (++pos[id] == (int)nums[id].size()) break;
heap.push(id);
mx = max(mx, (long long)nums[id][pos[id]]);
}
return {(int)bestLo, (int)bestHi};
}
public int[] smallestRange(List<List<Integer>> nums) {
int k = nums.size();
if (k == 0) return new int[0];
for (List<Integer> list : nums) if (list.isEmpty()) return new int[0];
int[] pos = new int[k];
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> {
int va = nums.get(a).get(pos[a]), vb = nums.get(b).get(pos[b]);
return va != vb ? Integer.compare(va, vb) : Integer.compare(a, b);
});
long max = Integer.MIN_VALUE;
for (int id = 0; id < k; id++) {
heap.add(id);
max = Math.max(max, nums.get(id).get(0));
}
long bestLo = nums.get(heap.peek()).get(0), bestHi = max;
while (true) {
int id = heap.poll();
long value = nums.get(id).get(pos[id]);
if (max - value < bestHi - bestLo) { bestLo = value; bestHi = max; }
if (++pos[id] == nums.get(id).size()) break;
heap.add(id);
max = Math.max(max, nums.get(id).get(pos[id]));
}
return new int[]{(int) bestLo, (int) bestHi};
}
def smallest_range(nums):
k = len(nums)
if k == 0 or any(len(lst) == 0 for lst in nums): return []
pos = [0] * k
heap = [] # (current value, list id)
mx = float('-inf')
for i in range(k):
heapq.heappush(heap, (nums[i][0], i))
mx = max(mx, nums[i][0])
best_lo, best_hi = heap[0][0], mx
while True:
_, i = heapq.heappop(heap)
value = nums[i][pos[i]]
if mx - value < best_hi - best_lo: best_lo, best_hi = value, mx
pos[i] += 1
if pos[i] == len(nums[i]): break
heapq.heappush(heap, (nums[i][pos[i]], i))
mx = max(mx, nums[i][pos[i]])
return [best_lo, best_hi]
function smallestRange(nums: number[][]): number[] {
const k = nums.length;
if (k === 0 || nums.some((list) => list.length === 0)) return [];
const pos = new Array<number>(k).fill(0);
const heap = new Heap<number>((a, b) => nums[a][pos[a]] < nums[b][pos[b]] || (nums[a][pos[a]] === nums[b][pos[b]] && a < b));
let max = -Infinity;
for (let id = 0; id < k; id++) {
heap.push(id);
max = Math.max(max, nums[id][0]);
}
let bestLo = nums[heap.peek()][0], bestHi = max;
for (;;) {
const id = heap.pop();
const value = nums[id][pos[id]];
if (max - value < bestHi - bestLo) { bestLo = value; bestHi = max; }
if (++pos[id] === nums[id].length) break;
heap.push(id);
max = Math.max(max, nums[id][pos[id]]);
}
return [bestLo, bestHi];
}
class Heap<T> {
private a: T[] = [];
constructor(private before: (x: T, y: T) => boolean) {}
get size() { return this.a.length; }
peek(): T { return this.a[0]; }
push(v: T) {
const a = this.a;
a.push(v);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.before(a[i], a[p])) break;
[a[i], a[p]] = [a[p], a[i]];
i = p;
}
}
pop(): T {
const a = this.a;
const top = a[0];
const last = a.pop()!;
if (a.length) {
a[0] = last;
let i = 0;
for (;;) {
let m = i;
const l = 2 * i + 1, r = l + 1;
if (l < a.length && this.before(a[l], a[m])) m = l;
if (r < a.length && this.before(a[r], a[m])) m = r;
if (m === i) break;
[a[i], a[m]] = [a[m], a[i]];
i = m;
}
}
return top;
}
}