[LintCode] Identify Celebrity


Suppose you are at a party withnpeople (labeled from0ton - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the othern - 1people know him/her but he/she does not know any of them.

Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).

You are given a helper functionbool knows(a, b)which tells you whether A knows B. Implement a functionint findCelebrity(n), your function should minimize the number of calls toknows.

There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return-1.

Example

Given n =2

2 // next n * (n - 1) lines 
0 knows 1
1 does not know 0

return1// 1 is celebrity

Solution 1. O(n^2) runtime

The obvious solution of this problem is for each person, check if he/she does not know any other person and all the rest know him/her.

It takes O(n) time to check if a person is a celebrity or not as it needs to call knows(himself, b) then knows(b, himself) for each of the rest n - 1 people.

So the runtime is O(n^2).

Solution 2. O(n) runtime

For function knows(A, B), if it returns true, it means A knows B; then A can't be a celebrity since A knows B.

if it returns false, it means A does not know B; then B can't be a celebrity since B is not known by A.

In solution 1, when calling knows(a, b), the algorithm only uses either a false return of knows(himself, b) or a true return of knows(b, himself);

The indirect information we get out of knows(himself, b) == true or knows(b, himself) == false is not used.

If knows(himself, b) == true --> himself can't be a celebrity; If knows(b, himself) == false, himself can't be a celebrity.

So each time we call knows(a,b), we can eliminate one candidate from the total possible candidates.

A O(n) algorithm is devloped as the following.

1. set possible celebrity to 0, for i from 1 to n - 1, check if the possible celebrity candiate knows the current person i;

if he knows i, update possible celebrity to i(eliminates the old candidate as he knows i). This is done in O(n) time.

2. step 1 provides the possible candidate. The algorithm then go through all peope and check if this candidate is known

by every one and he does not know any one, in O(n) time.

 1 /* The knows API is defined in the parent class Relation.
 2       boolean knows(int a, int b); */
 3 public class Solution extends Relation {
 4     /**
 5      * @param n a party with n people
 6      * @return the celebrity's label or -1
 7      */
 8     public int findCelebrity(int n) {
 9         if(n <= 0){
10             return -1;
11         }
12         if(n == 1){
13             return 0;
14         }
15         int curr = 0;
16         for(int i = 1; i < n; i++){
17             if(knows(curr, i)){
18                 curr = i;
19             }
20         }
21         for(int i = 0; i < n; i++){
22             if(curr == i){
23                 continue;    
24             }
25             if(knows(curr, i)){
26                 return -1;
27             }
28             else if(knows(i, curr) == false){
29                 return -1;
30             }
31         }
32         return curr;
33     }
34 }

优质内容筛选与推荐>>
1、MSSQL 数据库语句原来是区分大小写的啊
2、showModalDialog/showModelessDialog实例,父窗口向子窗口传递值,子窗口设置父窗口的值
3、kafka connect,将数据批量写到hdfs完整过程
4、移动viewport的相关事件
5、通过汇编简单C程序来理解计算机如何工作


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn