查找多个跳过列表的交叉点

发布于 2025-02-04 03:27:59 字数 422 浏览 2 评论 0原文

这是用于查找两个跳过列表的交点的算法:

”在此处输入图像描述” 查找两个跳过列表的交叉点 - 对斯坦福大学的版权

我们可以看到,与一次移动一步相比,“跳过跳跃”在效率方面受益匪浅。

但是在这里我很好奇,如果将这种情况扩展到多个跳过列表,说100个列表怎么办?目前,我只考虑分裂和征服,其中多个跳过列表按2分组,并顺序得出其相交,然后合并解决方案,这听起来很耗时且效率低下。

确定多个跳过列表的相交以及花费最少的时间的更好方法是什么?

This is the algorithm for finding the intersection of two skip lists:

enter image description here
(Finding Intersection of two skip lists - copyright to Stanford)

We can see that the "jumping by skips" benefits a lot in terms of efficiency compared to moving one step at a time.

But here I'm curious, what if the case is extended to multiple skip lists, say 100 lists? Currently, I only think of divide and conquer, in which the multiple skip lists are grouped by 2, and sequentially derive its intersection and later merge the solution, which sounds time-consuming and inefficient.

What is the better way to determine the intersections of multiple skip lists with the least time spent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2025-02-11 03:27:59

初始化一个指针,直达每个跳过列表的开头。

我们将维护两件事:

  • 当前的最大值指向
  • (值,指针)对的最小值。

在每个步骤:

  • 检查所有指针是否具有相同的值,通过将Min-Heap的顶部与最大值进行比较。
    • 如果这两个是相同的:
      • 所有当前值必须相同(因为min == max),因此该值在交点中。
      • 将该值添加到输出中。
      • 弹出您的小矿床,将其指针推进,直到达到更大的价值,然后推动新价值。将最大最大更新为新值。
    • 其他:
      • 弹出您的小矿山,推进其指向最大值的指针,根据需要跳过。
      • 如果其新值超过最大值,请更新最大值。
      • 将新价值推向您的遥远。

当任何列表耗尽时停止(您需要推进指针,但不能推进。)

这是经典编程面试问题“合并K分类列表”的略有变化 - 这里的算法非常相似。我建议看看这个答案中的任何东西尚不清楚。

Initialize a pointer to the beginning of each of your skip lists.

We will maintain two things:

  • The current max value pointed to
  • a min-heap of (value, pointer) pairs.

At each step:

  • Check if all pointers have the same value by comparing the top of the min-heap with the max value.
    • If those two are are the same:
      • All current values must be the same (since min == max), so the value is in the intersection.
      • Add that value to the output.
      • Pop your min-heap, advance its pointer until it gets to a bigger value, and push the new value. Update max to the new value.
    • Else:
      • Pop your min-heap, advance its pointer towards the max value, skipping as needed.
      • If its new value exceeds the max value, update the max value.
      • Push the new value onto your min-heap.

Stop when any list runs out (you need to advance a pointer but can't.)

This is a slight twist on a classic programming interview problem "Merge k sorted lists" -- the algorithm here is very similar. I'd suggest looking at that if anything in this answer is unclear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文