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.

分析:

我们从0开始,依次问他是否认识他的下一个,比如 1, 如果 0 说认识,那么 0 一定不是名人,1 有可能是名人; 如果0 说不认识,1 一定不是名人,0 却有可能是名人。这是这个方法巧的地方。所以,不管1 回答是还是不是,我们都可以确定其中一个不是名人,然后,我们继续问 可能是名人那一位 是否认识 2, 然后依次下去,直到第 N - 1个人。这样我们就可以只要问 (N - 1) 个问题就可以把名人找出来。

     int findCelebrity(int n) {
int c = ;
for (int i = ; i < n; ++i) {
if (knows(c, i)) {
c = i;
}
} for (int i = ; i < n; ++i) {
if (c != i && (knows(c, i) || !knows(i, c)))
return -;
}
return c;
}

Find celebrity的更多相关文章

  1. [LeetCode] 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 ...

  2. LeetCode Find the Celebrity

    原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...

  3. 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 ...

  4. Poj 3062 Celebrity jeopardy

    1.Link: http://poj.org/problem?id=3062 2.Content: Celebrity jeopardy Time Limit: 1000MS   Memory Lim ...

  5. [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 ...

  6. [Locked] Find the Celebrity

    Find the Celebrity Suppose you are at a party with n people (labeled from 0 to n - 1) and among them ...

  7. Find the Celebrity 解答

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

  8. 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 ...

  9. Uva 01124, POJ 3062 Celebrity jeopardy

    It's hard to construct a problem that's so easy that everyone will get it, yet still difficult enoug ...

随机推荐

  1. shutdown命令用法

    首先我们先创建一个txt文件,添加shutdown -r -f -t 0 ,文件点击另存为,选择所有类型,保存格式为“重启.bat”文件. 说明:shutdown命令用法: /r         关闭 ...

  2. Nginx IP访问控制,只允许指定的IP地址访问

    Nginx可以进行IP访问控制,配置指定的IP地址访问服务器网站 今天领导提出一个新的业务需求,网站上线时让外部用户在上线时间段访问到的页面是维护页面,公司内部员工在上线时段可用正常访问公司的网站. ...

  3. 今天是JQ 的slideUp 和 slideDown 的点击事件

    先贴代码,再讲详细事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  4. Java Programming Test Question 1

    public class JPTQuestion1 { public static void main(String[] args) { String s1 = "abc"; St ...

  5. 如何在html结构标签中使用js 变量 生成可变化的 title标题?

    如果form的action不写, 或 action="", 那么就表示 将数据发送到 本文件 当前文件自身... 1. 在jquery的选择器中, $()括号中的内容是一个 exp ...

  6. 全屏背景:15个jQuery插件实现全屏背景图像或媒体

    动态网站通常利用背景图像或预加载屏幕,以保证所有资源都加载到页面上,在浏览器中充分呈现.现在很多网站都炫耀自己的图像作为背景图像全屏背景,追溯到旧的Flash网站却用自己的方式在HTML资源重布局. ...

  7. NetBeans使用习惯:升级与保存配置

    如何升级:点击 netbeans 的升级更新 ,即可升级版本:不推荐官网下载进行安装,否则会出现,以前的旧版本8.0的目录和8.0.1目录,虽然它会自动检测到以前版本的配置,提示导入... 如何备份: ...

  8. [译]git rebase

    rebase就是重新定义你分支的起点, 分支上的commit将生成对应的新的commit并放在你指定的新的起点commit后, 分支上的老commit将被删除. rebase就是将你的分支从一个com ...

  9. HDOJ 4497 GCD and LCM

    组合数学 GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  10. Smarty基础

    smarty将php代码和HTML代码分开,形成两个页面,通过在php页面引用smarty配置文件,利用php控制HTML页面显示 1,libs文件夹,放入需要使用的文件夹下面 2,配置文件:init ...