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.