algorithm - 以最有效的方式将几个预先排序的列表合并到一个联合列表中(并获取其中的顶部元素)

标签 algorithm list linked-list

我有这样一个预设:

  • 许多预先排序的有序列表,例如 1000 个元素,每个元素按标准排序(例如,基于时间的“最后一个”)
  • 需要制作一个联合列表,该列表保持排序顺序并且还包含 1000 个最后的元素(因此可以丢弃不适合前 1000 个的原始列表的元素)。 但是,也可以单独选择前 1000 个。
  • 合并需要尽可能快、尽可能高效。重新排序完整的合并列表不是一个选项。

最佳答案

使用任何priority queue基于数据结构:

priority queue q = empty
for each list add first element to q
create an array next that contains next elements for every list (initially next element is a second element)

while result list is not full
    take top element from the q and add to the result list
    add next element of the corresponding list to the q (if any)
    update next element of the corresponding list

关于algorithm - 以最有效的方式将几个预先排序的列表合并到一个联合列表中(并获取其中的顶部元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54781976/

相关文章:

android - fragment 它传递数据但不更新列表适配器

c++ - 为链接列表建立条目类

c++ - 如何解决RemoveAStud()中未删除节点并崩溃的问题

algorithm - 三个顶点上有多少个无向图?

c++ - 在 openGL 中禁用图像剔除

image - 在图像中找到相似区域的好算法?

python - 将项目添加到嵌套字典

CSS - 元素符号和列表之间的空间而不填充列表

java - Java 链表中的指针

string - 给定一个回文字符串,通过从中再删除一个字符,我们可以通过多少种方式将其转换为非回文字符串?