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.


题目标签:Array

  题目给了我们一个n, 代表 有n个人,从0 到n-1。其中可能会有一个明星,或者没有明星,让我们通过,问每个人问题的方式,找到谁是明星或者没人是明星。问题的方式是:问A,你认识B吗? 可以得到是 或者 不是的回答。明星的定义是:明星不认识任何人,但是所有人都要认识明星。

  我们可以利用遍历n两次来找出谁是明星:

  第一次遍历:假设第一个人是明星;所有人站一排0 到 n-1。

        依次问每一个人X,你认识下一个人吗?

          如果认识: 假设下一个人是明星;

          如果不认识:假设不变,X依然是。

  如果有明星再这一排人里的话,当问到明星前面一个人的时候,肯定会把明星设为假设,而之后问明星 认不认识 之后的所有人的时候,得到的肯定是 否定的回答,所以假设会留在明星这里。

  第二次遍历:因为不一定有明星,所以还要遍历一次来确定假设的是不是明星。

        依次问每一个人X(除了假设的明星之外), X认识假设的明星吗 还要问 假设的明星 认识X吗?

          如果不是真的明星,他/她 不会被所有人认识 或者 他/她 会认识一些人。

Java Solution:

Runtime beats 80.26%

完成日期:09/09/2017

关键词:Array

关键点:需要2次遍历:1次 用来挑出 候选; 1次 用来验证 候选

 /* The knows API is defined in the parent class Relation.
boolean knows(int a, int b); */ public class Solution extends Relation
{
public int findCelebrity(int n)
{
int candidate = 0; // first iteration: find out the candidate
for(int i=1; i<n; i++)
{
if(knows(candidate, i))
candidate = i;
} // second iteration: make sure candidate is a celebrity
for(int i=0; i<n; i++)
{
if(i == candidate) // no need to ask candidate
continue; // everyone else should know celebrity and celebrity should not know anyone else
if(!knows(i, candidate) || knows(candidate, i))
return -1;
} return candidate;
}
}

参考资料:

https://leetcode.com/problems/find-the-celebrity/discuss/

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 277. Find the Celebrity (找到明星)$的更多相关文章

  1. 名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)

    1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...

  2. [LeetCode] 277. Find the Celebrity 寻找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  3. [LeetCode#277] Find the Celebrity

    Problem: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...

  4. [leetcode]277. Find the Celebrity 找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  5. [leetcode]277. Find the Celebrity谁是名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  6. 【LeetCode】277. Find the Celebrity 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  7. 277. Find the Celebrity

    题目: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exi ...

  8. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

  9. [LeetCode] Find Eventual Safe States 找到最终的安全状态

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

随机推荐

  1. [js高手之路]Node.js模板引擎教程-jade速学与实战4-模板引用,继承,插件使用

    一.block 模块复用 把需要复用的模块用block定义 block后面跟上模块的名字,引用一次block 内容就会被复用一次 编译之后的结果: 二,继承模板(extends) 在实际开发中,网站的 ...

  2. 25个最基本的JavaScript面试问题及答案

    1.使用 typeof bar === "object" 来确定 bar 是否是对象的潜在陷阱是什么?如何避免这个陷阱? 尽管 typeof bar === "objec ...

  3. 浅谈SQL优化入门:1、SQL查询语句的执行顺序

    1.SQL查询语句的执行顺序 (7) SELECT (8) DISTINCT <select_list> (1) FROM <left_table> (3) <join_ ...

  4. 一个JavaScript触发器插件,可通过指定频次、指定时间内触发指定的处理函数

    js-trigger是一个JavaScript触发器插件,可通过指定频次.指定时间内触发指定的处理函数 Tango<tanwei_yx@126.com> 特性 支持AMD/CMD/Comm ...

  5. codeigniter 去除index.php (nginx,apache) 通用方法

    .htaccess文件配置 1 <IfModule mod_rewrite.c> 2 RewriteEngine On 3 RewriteBase / 4 RewriteCond $1 ! ...

  6. asp.net core合并压缩资源文件引发的学习之旅

    0. 在asp.net core中使用BuildBundlerMinifier合并压缩资源文件 在asp.net mvc中可以使用Bundle来压缩合并css,js 不知道的见:http://www. ...

  7. C++中const用于函数重载

    C++中const用于函数重载 常成员函数和非常成员函数之间的重载 首先先回忆一下常成员函数 声明:<类型标志符>函数名(参数表)const: 说明: (1)const是函数类型的一部分, ...

  8. Qt实现基本QMainWindow主窗口程序

    这个实验用Qt实现基本QMainWindow主窗口 先上实验效果图    打开一个文件,读取文件类容 详细步骤: 1.打开Qt creator新建MainWindow工程 右键工程名添加新文件,mai ...

  9. Linux入门之常用命令(12)用户管理

    [用户管理] linux如何查看所有的用户和组信息的方法: 1.cat /etc/passwd: 2.cat /etc/group 1. useradd useradd 命令可以创建一个新的用户帐号, ...

  10. 2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...