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. sqlserver检测死锁;杀死锁和进程;查看锁信息

    http://blog.sina.com.cn/s/blog_9dcdd2020101nf4v.html sqlserver检测死锁;杀死锁和进程;查看锁信息 ( ::)转载▼ 标签: sql 检测死 ...

  2. Struts2的模板和主题theme及自定义theme的使用

    Struts2的模板和主题theme及自定义theme 标签: struts2 2016-03-29 11:22 190人阅读 评论(0) 收藏 举报  分类: javaweb(8)  Struts2 ...

  3. C-基本语法与运算

    编译: Technorati 标记: C 1, 编译compilers 命令make 将高级语言转换为低级语言. clang: 1,预处理(preprocessing) 2,编译(complition ...

  4. Centos6.5搭建java开发环境

    一.安装jdk 1.查看Linux自带的JDK是否已安装 java –version 如果出现openjdk,最好还是先卸载掉openjdk,在安装sun公司的jdk. 2.查看jdk信息 rpm - ...

  5. Mono资源

    摘要 最近看了一部分mono方面的资料,这里整理一下,在这里列一个目录,方便以后用到的时候查找. Mono Mono 是一个由 Xamarin 公司(先前是 Novell,最早为 Ximian)所主持 ...

  6. 【转】快速理解Kafka分布式消息队列框架

     from:http://blog.csdn.net/colorant/article/details/12081909 快速理解Kafka分布式消息队列框架 标签: kafkamessage que ...

  7. Linux内核循环链表经典分析和移植

    为什么说这个链表做的经典呢,哥哥我从Linux内核里边儿扣出来的,要么怎么说内核不是一般人能写的,这代码太TM优美了! 这里有一篇参考文章:http://isis.poly.edu/kulesh/st ...

  8. SQL 获取查询IO信息

    DBCC DROPCLEANBUFFERS --清空缓存 SET STATISTICS IO ON --开启IO统计 SET STATISTICS TIME ON -- 开启耗时统计 <code ...

  9. RelativeLayout布局

    RelativeLayout用到的一些重要的属性: 第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:layout_center ...

  10. Mahout 介绍

    1.Hbase+k-means  (G级别) 2.k-means+mr (T级别) 1. 2.canopy 2.贝叶斯算法 决策,分类,文档分类 3.推荐系统 4.图书推荐系统 1.需求 付完款的用户 ...