Equivalence testing of n objects

前端 未结 4 1250
一向
一向 2021-01-07 05:20

Assume that we are given \'n\' objects and a subroutine that takes two inputs and says if they are equivalent or not (e.g. it can give output as 1 if they are equal).

<
4条回答
  •  自闭症患者
    2021-01-07 05:46

    Paul Hankin's answer is a $\mathcal{O}(n)$ solution and very fancy.

    If do not use Boyer-Moore, we can also solve this problem in $\mathcal{O}(nlogn)$. Every time after comparision, we "drop" the different pair of cards and "merge" the equal pair to a group, and so on, every time we drop the same number of cards, the final left card or card group is the "Majority", then we can to run the comparision again to check is card is True or not.

    As every time we drop, we at least drop a majority card and another, so it will not influence the result, the process of drop is a "offset" operation. Not matter drop or merge, help us reduce at least half of the card. So the recursive times is $logn$, the total time is $nlogn$.

提交回复
热议问题