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 ...
随机推荐
- Python之路【第七篇续】:I/O多路复用
回顾原生Socket 一.Socket起源: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用[打开][读写][关闭]模式来操作. socket就是该模式的 ...
- OC-点语法
点语法的本质:方法调用 #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, ...
- Yii2的深入学习--自动加载机制(转)
Yii2 的自动加载分两部分,一部分是 Composer 的自动加载机制,另一部分是 Yii2 框架自身的自动加载机制. Composer自动加载 对于库的自动加载信息,Composer 生成了一个 ...
- php生成唯一随机码
最终使用: echo md5(time() . mt_rand(1,1000000)) //A:利用时间戳的方法 md5("admin"); // B:32位MD5加密 subst ...
- spring的PathMatchingResourcePatternResolver-通配符的Resource查找器
PathMatchingResourcePatternResolver是一个通配符的Resource查找器,包括: /WEB-INF/*-context.xml com/mycompany/**/ap ...
- Class.isAssignableFrom(Class clz)与instanceof与Class.isInstance(Object obj) 的区别和联系
编程的时候可能会遇到一个不知道它属于哪个类的对象,我们可以用下列运算符或者方法来判断. 1.instanceof instanceof是运算符只被用于对象引用变量,检查左边的被测试对象是不是右边类或 ...
- [asp.net core]project.json(1)
摘要 前面介绍了使用vs2015新建asp.net core web的内容,这篇文章学习下project.json文件的内容. project.json 原文:https://docs.microso ...
- php后台多用户权限组思路与实现程序代码
网站开发少不了有网站后台,有了后台自然要对用户有同角色来分配一下,特别是多用户系统的情况下,如我一个系统要有多个管理员,那么我这些管理要分成,编辑,友情连接,管理员等,那我们要有权限和角色分配,今天我 ...
- C#委托全解析
什么是委托? 委托类似于C语 ...
- golang的json操作
package main import ( "encoding/json" "fmt" "os" ) type ConfigStruct s ...