【一道算法题】寻找N个数中重复数量大于 N/2的数

版权声明:其他网站转载请注明原始链接,尽量不要破坏格式 https://blog.csdn.net/landstream/article/details/78927269

Question

Suppose you’re consulting for a bank that’s concerned about fraud detection, and they come to you with the following problem. They have a collection of n bank cards that they’ve confiscated, suspecting them of being used in fraud. Each bank card is a small plastic object, containing a magnetic stripe with some encrypted data, and it corresponds to a unique account in the bank. Each account can have many bank cards corresponding to it, and we’ll say that two bank cards are equivalent if
they correspond to the same account. It’s very difficult to read the account number off a bank card directly,
but the bank has a high-tech “equivalence tester” that takes two bank cards and, after performing some computations, determines whether they are equivalent.
Their question is the following: among the collection of n cards, is there a set of more than n/2 of them that are all equivalent to one another?

Assume that the only feasible operations you can do with the cards are to pick two of them and plug them in to the equivalence tester. Show how to decide the answer to their question with only O(n log n) invocations of the equivalence tester.

Algorithm Design - Jon Kleinberg , P246

这是一道分治策略章节课后习题,不过课上同学提出了一种线性复杂度的方法,我对算法的研究不多,因此格外感兴趣。

总的思想是系统通过对撞,消除每一对不同的数,留下重复次数最多的数。

手工验算草图如下:
圆圈中的为删除的元素。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/landstream/article/details/78927269