277. Find the Celebrity

Problem

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.

Note: 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.

Solution

遍歷每一個(gè)人,看其余的n-1個(gè)人是否都認(rèn)識(shí)這個(gè)人,如果是,那么如果這個(gè)人不認(rèn)識(shí)任何人則輸出,否則繼續(xù)下一個(gè)人。

// Forward declaration of the knows API.
bool knows(int a, int b);

class Solution {
public:
    int findCelebrity(int n) {
        for(int i = 0; i < n; i++) {
            bool flag = false;
            for(int j = 0; j < n; j++) {
                if (i != j && !knows(j, i)) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                flag = false;
                for(int j = 0; j < n; j++) {
                    if (i != j && knows(i, j)) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    return i;
                }
            }
        }
        return -1;
    }
};

優(yōu)化

O(n).

以下是第一重循環(huán)的解釋。

Let's say i = k, you first time switch candidate, then 0 ~ k -1 will not have candidate. 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. So if the pre candidate(0) doesn't know 1 ~ k-1, then 1 ~ k - 1 are not celebrity. As you just switch candidate, then 0 is not.

code

// Forward declaration of the knows API.
bool knows(int a, int b);

class Solution {
public:
    int findCelebrity(int n) {
        int candidate = 0;
        // If candidate does not know k (0 ~ i-1). It means that k cannot be celebrity, since celebrity
        // must be known by everyone.
        for(int i = 1; i < n; i++) {
            if (knows(candidate, i)) {
                candidate = i;
            }
        }
    
        for(int j = 0; j < n; j++) {
            if (candidate != j && (knows(candidate, j) || !knows(j, candidate))) {
                return -1;
            }
        }
    
        return candidate;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,891評(píng)論 0 23
  • 整體櫥柜主要是由櫥柜、電器、燃?xì)饩摺N房功能用具四位一體組成的廚柜組合。隨著整體櫥柜的普及,人們開始對(duì)廚房有了全新...
    世傳家居閱讀 239評(píng)論 0 0
  • 霓虹的光 晃眼 特別是在晚上 我是不是喜歡一個(gè)人 也喜歡上了她的顏色 安靜的天藍(lán) 亦或是 魅惑的粉紅 打在燈上 酒...
    嵐峰閱讀 308評(píng)論 0 1
  • 創(chuàng)業(yè)需要一條道走到黑。 大家好,這里是創(chuàng)業(yè)導(dǎo)師阿勇,我是陳昌文,今天與大家聊的話題是:創(chuàng)業(yè)需要一條道走到黑。 不忘...
    阿勇同路人閱讀 262評(píng)論 0 0
  • 晚11:58 “狗男女!”一名持酒女人在大街上潑罵,“她有什么好的!不就一婊子嗎?”她的腦子回憶著剛剛回家時(shí)看到的...
    安梓_逸閱讀 492評(píng)論 0 0