Find the Celebrity

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.

分析:

  名流的定义竟然是其他所有人都知道他,而他却不知道其他任何人!好吧,承认也差不多是这么回事。言归正传,根据定义,名流首先需要保证所有人都知道他,所以复杂度至少为O(n);暴力解法为两两都问两遍,复杂度为O(n^2)。可以转换下思路,我们可以先去掉所有不可能是名流的人,然后对剩下O(1)个人,进行O(n)复杂度的检验。我想到的其中一个做法是,将n个人分为n/2组,每组两两问一遍,这样一轮就会至少去掉一半的人;然后循环迭代。复杂度为n + n/2 + n/4 +... = 2n。最后最多剩下一个人,进行2(n - 1)次经验。故总复杂度为2n + 2(n - 1) = O(n)。

代码:

int findCelebrity(int n) {
vector<int> candidate1, candidate2;
for(int i = ; i < n; i++)
candidate1.push_back(i);
//根据名流的必要性条件,去除所有一定不是名流的人
while(candidate1.size() > ) {
for(int i = , j = ; j < candidate1.size(); i += , j += ) {
if(knows(i, j) && !knows(j, i))
candidate2.push_back(j);
else if(knows(j, i) && !knows(i ,j))
candidate2.push_back(i);
}
vector<int> ().swap(candidate1);
candidate1.swap(candidate2);
}
//直接去除光了,表明无名流
if(candidate1.empty())
return -;
int candidate = candidate1[];
for(int i = ; i < n; i++)
//验证充要条件不成立,表明无名流
if(i != candidate && (!knows(i, candidate) || knows(candidate, i)))
return -;
//满足充要条件
return candidate;
}

[Locked] Find the Celebrity的更多相关文章

  1. ORA-28000: the account is locked 账户被锁

    这种情况可能是因为你输入错误的用户名密码达到10次,oracle给你锁住了. 解决方法: 首先 ~bash$ sqlplus /nolog SQL> conn sys/sys as sysdba ...

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

  3. oracle遇到的锁异常,oralce record is locked by another user

    由于我在前不久的一次项目调试的时候,将一条数据的ID与另一条数据的ID相同了,但不知为什么没有报错,当在页面发现问题时,删除这条数据时就报错了,oralce record is locked by a ...

  4. 解决svn working copy locked问题

    标题:working copy locked 提示:your working copy appears to be locked. run cleanup to amend the situation ...

  5. LeetCode Find the Celebrity

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

  6. scott/tiger登录时提醒ora-28000 the account is locked

    scott/tiger登录时提示ora-28000 the account is locked在plsql developer中要是以scott/tiger登录时提示ora-28000 the acc ...

  7. Tomcat Start 报错 (COULD NOT DELETE MAY BE LOCKED BY ANOTHER PROCESS)

    jsp文件重命名后发布不起来了,提示文件被占用,原因是当前的java ee项目 与它引用的java项目 依赖了相同的jar包,删除了clean 再发布,问题解决,如有需要再引用回来 http://it ...

  8. How to acquire an Android phone with locked bootloader?

    As we know that some devices come with locked bootloaders like Sony, HUAWEI, hTC...If you try to unl ...

  9. Xcode cannot launch because the device is locked.

    When you plug in your iPhone, it will ask you to trust the computer. If you already trust and unlock ...

随机推荐

  1. The performance between the 'normal' operation and the 'shift' operation.

    First, I gonna post my test result with some code: //test the peformance of the <normal operation ...

  2. ^(bitwise exclusive Or).

    一个数,进行异或同一个数两次,将得到原来的数,例如: 6 ^ 4 ^ 4 = 6; 0000-0000-0000-0110 ^ 0000-0000-0000-0100 ---------------- ...

  3. 数据库导出导入操作(expdp,impdp)

    EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. 命令行: sqlplus/nolog connect username/password as sysd ...

  4. PHP算法 《树形结构》 之 伸展树(1) - 基本概念

    伸展树的介绍 1.出处:http://dongxicheng.org/structure/splay-tree/ A. 概述 二叉查找树(Binary Search Tree,也叫二叉排序树,即Bin ...

  5. JavaScript中style.left与offsetLeft的区别

    今天在制作焦点轮播图的时候,遇到一个问题,在使用style.left获取图片的位置时,怎么也获取不到.换用offsetLeft就能够成功获取到了.虽然实现了我想要的效果,但是还是不甘心啊,没有找到原因 ...

  6. Weinre在iOS 7上不工作的原因

    升级到iOS 7 后发现Weinre不能工作了,通过Safari调试发现报错如下: [Error] SecurityError: DOM Exception 18: An attempt was ma ...

  7. jQuery API中文文档

    jQuery API中文文档 http://www.css88.com/jqapi-1.9/category/events/event-handler-attachment/ jQuery UI AP ...

  8. CentOS 6.4编译安装淘宝web服务器Tengine

    Tengine 是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商城 ...

  9. .net 实例化对象

    定义TestClass,在调用的地方 TestClass a; 如果下面有引用 a.Property,VS会报错,而如果 TestClass a = null; 再次调用的话则不会报错. TestCl ...

  10. 关于ueditor1.4.3版复制section标签丢失class和style样式问题

    在复制微信的文章格式到ueditor时发现section标签中的style和class属性丢失,严重影响美观. 原文格式,排版清晰段落分明赏心悦目: 复制到ueditor后的格式...这跟原文是没法比 ...