【发布时间】:2015-04-05 06:42:26
【问题描述】:
我一直在试图弄清楚如何为 K 路合并实现以下算法。
Algorithm:
1)Initialize an array of size n*k.
2) Initialize a min heap of size k, to hold the smallest element of each array.
3) Add the smallest element from the minHeap into the output array.
4)Move the next element from the array from which the min element was derived onto the heap. // How would one implement this?
5) Repeat step 4 until all the arrays are empty and the minHeap is empty.
除了算法的第 4 步之外,我已经能够实现所有其他操作。如何跟踪从中提取了最小元素的数组?
【问题讨论】: