Merge k Sorted Lists
Problem (yeniden ifade)
k adet sıralı bağlı listeyi tek bir sıralı listede birleştir.
Sezgi
Min-heap ile k listenin güncel head’leri arasından her zaman en küçüğünü al.
Yaklaşımlar
Head'lerin min-heap'i
DoğrulanmadıFikir. (val, liste indeksi / düğüm) heap’i; min’i pop et, next’ini push et.
Yürüyüş. [[1,4,5],[1,3,4],[2,6]] → 1→1→2→3→4→4→5→6.
Trade-off. Heap vs böl-ve-yönet birleştirme.
Yardımcı kodu göster
/** A real binary heap with a caller-supplied "less" comparator: O(log n) push/pop. */
class Heap<T> {
private a: T[] = [];
private less: (x: T, y: T) => boolean;
constructor(less: (x: T, y: T) => boolean) {
this.less = less;
}
get size(): number {
return this.a.length;
}
peek(): T {
return this.a[0]!;
}
push(x: T): void {
const a = this.a;
a.push(x);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.less(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 (;;) {
const l = 2 * i + 1;
const r = l + 1;
let s = i;
if (l < a.length && this.less(a[l]!, a[s]!)) s = l;
if (r < a.length && this.less(a[r]!, a[s]!)) s = r;
if (s === i) break;
[a[i], a[s]] = [a[s]!, a[i]!];
i = s;
}
}
return top;
}
}
export class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next: ListNode | null = null) {
this.val = val;
this.next = next;
}
}
/** A real binary heap with a caller-supplied "less" comparator: O(log n) push/pop. */
class Heap<T> {
private a: T[] = [];
private less: (x: T, y: T) => boolean;
constructor(less: (x: T, y: T) => boolean) {
this.less = less;
}
get size(): number {
return this.a.length;
}
peek(): T {
return this.a[0]!;
}
push(x: T): void {
const a = this.a;
a.push(x);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.less(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 (;;) {
const l = 2 * i + 1;
const r = l + 1;
let s = i;
if (l < a.length && this.less(a[l]!, a[s]!)) s = l;
if (r < a.length && this.less(a[r]!, a[s]!)) s = r;
if (s === i) break;
[a[i], a[s]] = [a[s]!, a[i]!];
i = s;
}
}
return top;
}
}
export class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next: ListNode | null = null) {
this.val = val;
this.next = next;
}
}
export function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
// Real binary min-heap ordered by node value.
const heap = new Heap<ListNode>((a, b) => a.val < b.val);
for (const h of lists) if (h) heap.push(h);
const dummy = new ListNode(0);
let cur = dummy;
while (heap.size) {
const n = heap.pop();
cur.next = n;
cur = n;
if (n.next) heap.push(n.next);
}
return dummy.next;
}
export function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
// Real binary min-heap ordered by node value.
const heap = new Heap<ListNode>((a, b) => a.val < b.val);
for (const h of lists) if (h) heap.push(h);
const dummy = new ListNode(0);
let cur = dummy;
while (heap.size) {
const n = heap.pop();
cur.next = n;
cur = n;
if (n.next) heap.push(n.next);
}
return dummy.next;
}
İkili birleştirme
DoğrulanmadıFikir. Listeleri soldan sağa ikişer birleştir. Heap sürümü k-yönlü biçimdir; bu sürüm birleştirme ilkelidir.
Trade-off. Her katlamada uzayan önek kopyalanır, toplam O(Nk). Listeleri ağaçta eşlemek O(N log k). Mülakatta olağan seçim heap’tir.
Yardımcı kodu göster
export class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next: ListNode | null = null) {
this.val = val;
this.next = next;
}
}
export class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next: ListNode | null = null) {
this.val = val;
this.next = next;
}
}
function mergeTwo(a: ListNode | null, b: ListNode | null): ListNode | null {
const dummy = new ListNode(0);
let cur = dummy;
while (a && b) {
if (a.val <= b.val) {
cur.next = a;
a = a.next;
} else {
cur.next = b;
b = b.next;
}
cur = cur.next;
}
cur.next = a ?? b;
return dummy.next;
}
/** Merge lists left-to-right with two-list merge. */
export function mergeKListsPairwise(lists: Array<ListNode | null>): ListNode | null {
let acc: ListNode | null = null;
for (const h of lists) acc = mergeTwo(acc, h);
return acc;
}
function mergeTwo(a: ListNode | null, b: ListNode | null): ListNode | null {
const dummy = new ListNode(0);
let cur = dummy;
while (a && b) {
if (a.val <= b.val) {
cur.next = a;
a = a.next;
} else {
cur.next = b;
b = b.next;
}
cur = cur.next;
}
cur.next = a ?? b;
return dummy.next;
}
/** Merge lists left-to-right with two-list merge. */
export function mergeKListsPairwise(lists: Array<ListNode | null>): ListNode | null {
let acc: ListNode | null = null;
for (const h of lists) acc = mergeTwo(acc, h);
return acc;
}
Şablon bağlantısı
K-yönlü birleştirme: her listenin güncel head’ini tutan bir min-heap. En küçüğü pop et, sona ekle, o düğümün next’ini it. Heap en fazla k uzun kalır. İkili birleştirme, iki liste birleştirmesinin listeler boyunca tekrarlanmasıdır.
Yansıma
- Min-heap liste başlarını tutar. Pop edilen listenin
next’i itilir. Boş listeler yığına girmez. - Eşit değerler: karşılaştırıcı yalnızca sayıda karar vermeli. Düğümü düğümle kıyaslamak hata verir.
- Tüm listeler boş: null. Tek liste: kendisi. k = 1: o listenin sırası.