[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, 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的更多相关文章
- ORA-28000: the account is locked 账户被锁
这种情况可能是因为你输入错误的用户名密码达到10次,oracle给你锁住了. 解决方法: 首先 ~bash$ sqlplus /nolog SQL> conn sys/sys as sysdba ...
- [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 ...
- oracle遇到的锁异常,oralce record is locked by another user
由于我在前不久的一次项目调试的时候,将一条数据的ID与另一条数据的ID相同了,但不知为什么没有报错,当在页面发现问题时,删除这条数据时就报错了,oralce record is locked by a ...
- 解决svn working copy locked问题
标题:working copy locked 提示:your working copy appears to be locked. run cleanup to amend the situation ...
- LeetCode Find the Celebrity
原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...
- scott/tiger登录时提醒ora-28000 the account is locked
scott/tiger登录时提示ora-28000 the account is locked在plsql developer中要是以scott/tiger登录时提示ora-28000 the acc ...
- Tomcat Start 报错 (COULD NOT DELETE MAY BE LOCKED BY ANOTHER PROCESS)
jsp文件重命名后发布不起来了,提示文件被占用,原因是当前的java ee项目 与它引用的java项目 依赖了相同的jar包,删除了clean 再发布,问题解决,如有需要再引用回来 http://it ...
- 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 ...
- 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 ...
随机推荐
- Singleton设计模式的一种见解
单实例Singleton设计模式可能是被讨论和使用的最广泛的一个设计模式了,这可能也是面试中问得最多的一个设计模式了.这个设计模式主要目的是想在整个系统中只能出现一个类的实例.这样做当然是有必然的,比 ...
- SDWebImage 在多线程下载图片时防止错乱的策略
在我们使用sd的时候,对tableView 上cell得图片进行异步下载的时候会遇到这样一个问题: 由于cell的重用机制,在我们加载出一个cell的时候imageView数据源开启一个下载任务并返 ...
- 类和ID选择器的区别
学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...
- healthkit 记录每天用户的运动情况
//详细操作步骤 http://www.csdn.net/article/2015-01-23/2823686-healthkit-tutorial-with-swift //官方api https: ...
- [转]Mysql导入导出工具Mysqldump和Source命令用法详解
Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...
- 子树大小平衡树(Size Balanced Tree,SBT)操作模板及杂谈
基础知识(包括但不限于:二叉查找树是啥,SBT又是啥反正又不能吃,平衡树怎么旋转,等等)在这里就不(lan)予(de)赘(duo)述(xie)了. 先贴代码(数组模拟): int seed; int ...
- SGU 221.Big Bishops(DP)
题意: 给一个n*n(n<=50)的棋盘,放上k个主教(斜走),求能放置的种类总数. Solution : 同SGU 220,加个高精度就好了. code #include <iostre ...
- html 标签的嵌套规则
1. 块元素可以包含内联元素或某些块元素,但内联元素却不能包含块元素,它只能包含其它的内联元素: <div><h1></h1><p></p> ...
- js中的潜伏者之Arguments对象
argument 说明: 在JavaScript中,arguments是对象的一个特殊属性.arguments对象就像数组,但是它却不是数组.可以理解为他是潜伏者,通俗的说,就是你传的参数不一定按照参 ...
- python模块之socket
43.python模块之socket: Python在网络通讯方面功能强大,学习一下Socket通讯的基本方式 UDP通讯: Server: import socket port=8081 ...