找出所有人认识的人

Given a function KNOWS(A,B), which returns 1 if A knows B (and not necessarily the other way around) and 0 if A does not know B.

A Celebrity is one who does not know anyone,
and one who is known by everybody.

For a list of N people, find all celebrities in linear time.

如果A B都是名人,所以A认识B,B认识A,但是名人是不认识任何人的,所以只有一个名人。

考查所有的人。选出任意一人作为候选人:candidate。从剩余要考查的人中选出一个人A检查和candidate的关系,如果A C相互认识或者相互不认识,说明他们都不是名人,将candidate设为剩余人中的其他人。如果只有A认识C,A不是名人,排除。如果只有C认识A将candidate设为A。。。。。。。。。设置考查完两人间的关系后重新设置候选人,重复上述过程。直到所有人考查完,最后candidate为名人。


另一种解法:(翻译过来的)

使用快排的思想如何?使用一个随机的人,记为Pivot。考查剩余的人是否认识他,将认识他的人放在他的右边,不认识他的人放在他的左边。

检查Pivot是否是最右边的人,如果是他是名人。否则对他右边的人使用同样的步骤。

Well, Graph will work but I feel like making extra structure for nodes will be a little complex.

How about using QuickSort Algo. choose a random person let say Pivot.
ask all others whether they know him or not. people who know him will come to left of him and people don't know him will come to right side.

after asking everyone if Pivot is the rightmost then he is the celebrity ortherwise follow the same steps for right side people of Pivot(Pivot will be excluded from the set because right side people don't know him so he can't be celebrity).

Running time will be same as solving it with Graph.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值