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

遍历每一个人,看其余的n-1个人是否都认识这个人,如果是,那么如果这个人不认识任何人则输出,否则继续下一个人。

// 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;
    }
};

优化

O(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;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,751评论 0 23
  • 整体橱柜主要是由橱柜、电器、燃气具、厨房功能用具四位一体组成的厨柜组合。随着整体橱柜的普及,人们开始对厨房有了全新...
    世传家居阅读 349评论 0 0
  • 霓虹的光 晃眼 特别是在晚上 我是不是喜欢一个人 也喜欢上了她的颜色 安静的天蓝 亦或是 魅惑的粉红 打在灯上 酒...
    岚峰阅读 321评论 0 1
  • 创业需要一条道走到黑。 大家好,这里是创业导师阿勇,我是陈昌文,今天与大家聊的话题是:创业需要一条道走到黑。 不忘...
    阿勇同路人阅读 344评论 0 0
  • 晚11:58 “狗男女!”一名持酒女人在大街上泼骂,“她有什么好的!不就一婊子吗?”她的脑子回忆着刚刚回家时看到的...
    安梓_逸阅读 519评论 0 0

友情链接更多精彩内容