Find 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.
分析:
我们从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的更多相关文章
- [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 ...
- LeetCode Find the Celebrity
原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...
- 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 ...
- Poj 3062 Celebrity jeopardy
1.Link: http://poj.org/problem?id=3062 2.Content: Celebrity jeopardy Time Limit: 1000MS Memory Lim ...
- [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 ...
- [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 ...
- Find the Celebrity 解答
Question Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...
- 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 ...
- 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 ...
随机推荐
- 微型orm fluentdata
http://fluentdata.codeplex.com/documentation#Query
- MV*模式的个人理解
MV*模式主要解决的问题就是 View代码难以维护的问题. MV*模式将View中的逻辑分离出去,形成一个弱逻辑的易于维护的视图. MV*中的*是Model和View的桥梁,负责保持Model和Vie ...
- idea中的svn配置
idea的使用之svn篇--有图超详细 http://ylq365.iteye.com/blog/1955291
- C# volatile关键字
; public int GetAge() { return Age; } 如上例子,调用GetAge()得到的是“主”内存区域的Age数值.用volatile修饰后的变量不允许有不同于“主”内存区域 ...
- 修改ubuntu DNS的步骤/wget url报错: unable to resolve host address的解决方法
wget url 报错:unable to resolve host address ‘url’,显然是无法解析主机地址,这就能看出是DNS解析的问题.解决办法就是配置可用的dns 一般是修改成为谷歌 ...
- 看懂理解 keyboard中 , navigation的设置: 切换工作区和移动窗口到不同的工作区.
navigation中, 主要有两个方面的内容: 移动窗口到工作区 的shortcuts 切换工作区的shortcuts 首先清楚: 工作区workspace, fedora 23中 好像只有上下方向 ...
- 只允许wheel组到用户才能使用 su命令
su只是切换到root用户, 不改变当前目录: su - 切换到root和改变目录到/root 修改用户到组: usermod gpasswd wheel组相当于windows的 Administra ...
- 黑客攻防技术宝典Web实战篇(二)工具篇DVWA Web漏洞学习
DVWA是一个学习Web漏洞的很好的工具. DVWA全程是Damn Vulnerable Web Application,还有一个跟它一样好的工具尽在http://www.360doc.com/con ...
- Js设置及获取Cookie的方法
Login页面设置Cookie: <script type="text/javascript"> if(json.result=="true") { ...
- 可编辑select
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...