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. HTTP请求的header头解析

    Request Headers: 下图是我访问一个URL:http://www.hzau.edu.cn的一个header,根据具体实例来分析一下各部分的功能及其作用. Accept 作用: 浏览器端可 ...

  2. 最近使用 .NET Core 遇到的一些坑

    最近.NET Core升级到2.0后开始慢慢捣鼓的多了起来,但遇到了不少坑,所以特来记录下. 第一个坑  条件编译符 我们在编写一些方法的时候通常会为Debug模式增加一些输出日志等以便我们检查,也会 ...

  3. 笔记本win10安装node的尖酸历程。。。。。。

    在公司的电脑搭建vue环境分分钟搞定,周末闲的无聊给自己的电脑也搭建一个环境,谁知道这么多的问题,记录下这些问题,希望对那些安装node环境有问题的朋友一些帮助. 1.下载安装node 下载地址htt ...

  4. 数据库服务器构建和部署列表(For SQL Server 2012)

    前言 我们可能经常安装和部署数据库服务器,但是可能突然忘记了某个设置,为后来的运维造成隐患.下面是国外大牛整理的的检查列表. 其实也包含了很多我们平时数据库配置的最佳实践.比如TEMPDB 文件的个数 ...

  5. 机器学习实战K-近邻算法

    今天开始学习机器学习,第一章是K-近邻算法,有不对的地方请指正 大概总结一下近邻算法写分类器步骤: 1. 计算测试数据与已知数据的特征值的距离,离得越近越相似 2. 取距离最近的K个已知数据的所属分类 ...

  6. 认识jQuery的Promise

    先前了解了ES6的Promise对象,来看看jQuery中的Promise,也就是jQuery的Deferred对象. 打开浏览器的控制台先. <script> var defer = $ ...

  7. PS 软件操作应用处理——粒子化任务效果

      前  言 JRedu 上次分享中,给大家介绍了一些图片的处理方法,主要是通过滤镜里的功能,把图片处理成素描效果或者水彩画效果,营造出不同的氛围. PS是一款非常强大的软件,包含了非常多的功能,合成 ...

  8. Google Authenticator 如何集成(U盾的实现原理相同)

    Google Authenticator是一个类似U盾的二次验证工具,Google提供了它的开源客户端(https://github.com/google/google-authenticator)里 ...

  9. ServiceStack.Text / Newtonsoft.Json 两种json序列化性能比较

    JSON序列化现在应用非常多,尤其在前后端分离的情况下,平常大多数C#下都使用Newtonsoft.Json来操作,量少的情况下,还可以忽略,但量大的情况下就要考虑使用ServiceStack.Tex ...

  10. FPGA在电平接口领域的应用

    电子技术的发展,产生了各种各样的电平接口. TTL电平: TTL电平信号之所以被广泛使用,原因是因为:通常我们采用二进制来表示数据.而且规定,+5V等价于逻辑"1",0V等价于逻辑 ...