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. Using Friendly URLs in ASP.NET Web Forms

    Introduction Websites often need to generate SEO friendly URLs. In ASP.NET Web Forms applications, a ...

  2. php-fpm进程关闭与重启脚本详解(转)

    先来理解一下什么是php-fpm PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的. PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将 ...

  3. Java使用Jetty实现嵌入式Web服务器及Servlet容器

     Jetty是一个Java实现的开源的servlet容器,它既可以像Tomcat一样作为一个完整的Web服务器和Servlet容器,同时也可以嵌入在Java应用程序中,在Java程序中调用Jetty. ...

  4. [js/jquery]移动端手势拖动,放大,缩小预览图片

    摘要 有这样的需求需要在手机端预览图片的时候,实现图片的手势拖动,放大缩小功能.最终通过touch.js这个插件实现了效果. touch.js Touch.js是移动设备上的手势识别与事件库, 由百度 ...

  5. thinkphp-3

    有两种创建项目的方式: 一是用多个单入口文件, 一个入口文件对应着一个项目, 如前台/后台/会员中心等 二是用一个单入口, 创建项目分组 对于有多个入口文件的 情况, 配置文件的共享问题? 不管是前台 ...

  6. Java中为什么有abstract interface 修饰类?

    如果有人问你为什么有abstract interface 修饰类,答案一定是他看到的这种方式一定是反编译出来的结果.实际中abstract interface和interface修饰的类没有区别. 下 ...

  7. 清北学堂模拟day4 捡金币

    [问题描述]小空正在玩一个叫做捡金币的游戏.游戏在一个被划分成 n行 n列的网格状场地中进行.每一个格子中都放着若干金币,并且金币的数量会随着时间而不断变化. 小空的任务就是在网格中移动,拾取尽量多的 ...

  8. 基于iSCSI的SQL Server 2012群集测试(三)--SQL Server 2012群集安装总结

    5.SQL Server 2012群集安装总结 5.1 群集与非群集的安装区别总结 SQL Server虚拟名称: 非群集环境下,本地服务器的名称就是SQL Server服务器名称:但在群集环境下,由 ...

  9. 提交上了,却在iTunes Connect没有新版本的任何消息

    上架的时候,收到这样的邮件 This app attempts to access privacy-sensitive data without a usage description. The ap ...

  10. Mac Pro Office Word 2011 个性化设置

    操作系统:Mac Pro OS X 10.11.5 1.常用的几个操作: (1).视图 -> 功能区 (2).视图 -> 打印版式 (3).视图 -> 大纲 (4).视图 -> ...