[LintCode] Identify Celebrity

Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people 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 function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows.

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

return 1 // 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 }
原文地址:https://www.cnblogs.com/lz87/p/7027299.html