高效搜索在2单链表的共同节点与内存限制?高效、节点、链表、内存

2023-09-11 05:45:39 作者:长愿相随

假设你提供两个signly链表这勾搭在一些点。设计一个O(N + M)算法使用不超过O(1)内存,并且找到的第一个公共节点,其中m和n是从列表的头部的距离,以帮派点,分别为。

Suppose you're given two signly-linked lists which ganging up at some point. design a O(n+m) algorithm with use of no more than O(1) memory which finding the FIRST common node, where m and n are the distance from the head of the list, to the gang point, respectively.

我想打标访问节点,但后来意识到,它需要更多的比O(1)内存。 问题是很容易当你可以将整个清单,这里不允许使用,因为运行时间限制上运行。帮助= D

I thought of marking visited nodes, but then realized that it takes more than O(1) memory. Problem is easy when you can run on the entire list, which is not allowed here because of runtime limitation. help =D

推荐答案

我假设有一种方法来比较元素 X 从第一个列表元素从第二个列表的方式来发现他们是否指的是同一个元素,超越了黑帮点。

I assume there is a way to compare an element x from the first list with an element y from the second list in a way to find out if they refer to the same element, beyond the gang point.

比较 X1 从对第一个列表 Y1 Y2 ,系统的第二列表的前两个元素。 比较 Y1 X1 X2 。 比较 X2 Y2 ... Y4 。 比较 Y2 X2 ... X4 。 比较 X4 Y4 ... Y8 。 ... Compare x1 from the first list against y1 and y2, the first two elements of the second list. Compare y1 with x1 and x2. Compare x2 with y2 ... y4. Compare y2 with x2 ... x4. Compare x4 with y4 ... y8. ...

在一般的:

for q in 1, 2, 4, 8, 16, ...
    Compare x_q with y_q ... y_2q
    Compare y_q with x_q ... x_2q

基本上,每一步你双倍的搜索范围。比方说, M< = N 。然后,在某些时候有 M<问 N-M<问。现在,你会发现一个匹配 x_q == Y_(Q +纳米)。本场比赛是什么地方超越了黑帮点,从那里你只需要回到帮派点。这是 O(N + M)

Basically, in each step you double the search range. Let's say m <= n. Then at some point there is m<q and n-m<q. Now you will find a match x_q==y_(q+n-m). This match is somewhere beyond the gang point, from there you just have to go back to the gang point. This is O(n+m).

修改确定,在回去的一部分,不与单链表的工作。然而,这是一个小的技术问题,因为在这一点上,我们知道其中的差别纳米键,可以重新开始,并从一开始就再次搜索,比较 x_k Y_(K +纳米)所有 K = 1,2,3,... ,直到我们找到一个匹配。

Edit OK, the go back part doesn't work with a singly linked list. However, this is a minor technical issue, because at this point we know the difference n-m and can start over and search again from the beginning, comparing x_k with y_(k+n-m) for all k=1,2,3,... until we find a match.

EDIT2 的复杂性,证明了一个粗略的介绍。对于每个我们 2 * Q 比较。比方说,我们找到一个匹配的 Q = Q1 。然后总结了所有的 Q1 4 * Q1 比较干脆,几何级数的总和。现在我们只需要证明 Q1 = O(N + M)。我们知道有没有匹配到 Q = Q1 / 2 ,这意味着无论是 M&GT; Q1 / 2 N'→2 * Q1 / 2 ,否则将有一场比赛。总之这可以writen为 N + M&GT; Q1 / 2 Q1 2 *(N + M)。为了比较的次数,我们得到 4 * Q1&LT; 8 *(N + M),这是完全在 O(N + M)。在这里,我们总是假定 N'GT; = M ,不失一般性。最后,我们要添加的返回的一部分,但这是线性的一样,所以总的算法是 O(N + M),因为每一部分。

Edit2 The Complexity, a rough overview of the proof. For each q we do 2*q comparisons. Let's say we find a match for q=q1. Then summing over all q up to q1 gives 4*q1 comparisons altogether, sum of the geometric series. Now we just need to prove q1=O(n+m). We know there is no match up to q=q1/2, this means either m>q1/2 or n>2*q1/2, because otherwise there would be a match. Together this can be writen as n+m>q1/2 or q1<2*(n+m). For the number of comparisons we get 4*q1<8*(n+m), which is well within O(n+m). Here we always assume n>=m, without loss of generality. Finally, we have to add the go back part, but this is linear as well, so the total algorithm is O(n+m) because each part is.

 
精彩推荐
图片推荐