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. Android-自定义多TAB悬浮控件实现蘑菇街首页效果

    因为项目的一些需求需要用到此种展现方式.  找了市面上大部分有类似功能的应用.  基本思路嵌套ScrollView 转换事件分发给listview 实现. 但是此种方案有个缺点. 在ScrollVie ...

  2. Fluent NHibernate之旅

    Fluent NHibernate 之旅 导航篇: [原创]Fluent NHibernate之旅开篇: [原创]Fluent NHibernate之旅二--Entity Mapping: [原创]F ...

  3. 对 Xcode 菜单选项的详细探索(转)

    转自 http://www.cnblogs.com/dsxniubility/p/4983614.html 本文调研Xcode的版本是 7.1,基本是探索了菜单的每一个按钮.虽然从xcode4一直用到 ...

  4. latex引用多篇参考文献

    1.如何使连续的参考文献能够中间用破折号连起来?比如[6,7,8,9]变成[6-9]? 方法:在文档开始前加上下面的语句命令 \usepackage[numbers,sort&compress ...

  5. cocos2dx 实现华丽丽的滚动层.

    前言 好久没写博客了. 前几周策划要求实现一个比较多功能的滚动层控件. 这个艰巨的任务就这样自然而然的落在了我这小身板上. 当然了, 只要我出手, 难度再高的需求也变得不堪一击. 哈哈哈哈 示例图 该 ...

  6. CSS实现DIV三角形

    本文内容收集来自网络 #triangle-up { width:; height:; border-left: 50px solid transparent; border-right: 50px s ...

  7. opencv数据结构总结

    OpenCV里面用到了很多图像相关的数据结构,熟练掌握它们是学习图像的基础. 1.IplImage IplImage IplImage IPL 图像头 typedef struct _IplImage ...

  8. 高级停靠(Dock)技术的实现

    高级停靠(Dock)技术的实现 介绍 所谓停靠就是可以用鼠标拖动窗体或者控件,并将其从一个父窗体移出或者移动到另一个父窗体上,可以按水平,垂直方向整齐排列, 并且可以停靠在分页控制组件上.下面的示意图 ...

  9. Django Admin 简单部署上线

    前言 打算为公司弄一个管理公用密码的平台,由于比较懒,就选择使用Django admin,默认的admin并不漂亮,于是我使用了这个django-suit插件来美化 如图: 是不是比原来的漂亮多了. ...

  10. ACM组队安排

    Problem Description   ACM亚洲区比赛结束,意味着开始备战明年的浙江省大学生程序设计竞赛了!  杭州电子科技大学ACM集训队也准备开始组队.  教练想把所有的n个队员组成若干支队 ...