Find the Celebrity 解答
Question
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
.
Solution
这题的关键是排除法。A知道B,那么我们就可以知道A一定不是candidate。所以我们就看B知道谁。
两层循环。
第一层找到candidate。
因为有且仅有一个celebrity,而且所有人都知道celebrity。所以如果有celebrity,通过这层循环一定可以找到它。
第二层验证candidate是否符合celebrity条件。
/* The knows API is defined in the parent class Relation.
boolean knows(int a, int b); */ public class Solution extends Relation {
public int findCelebrity(int n) {
int candidate = 0;
// Exclusion way to find candidate
for (int i = 1; i < n; i++) {
if (knows(candidate, i)) {
candidate = i;
}
}
// Check whether candidate is valid
for (int i = 0; i < n; i++) {
if (i == candidate) {
continue;
}
if (!knows(i, candidate) || knows(candidate, i)) {
return -1;
}
}
return candidate;
}
}
Find the Celebrity 解答的更多相关文章
- 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 ...
- [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 ...
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
- 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...
- 【字符编码】Java字符编码详细解答及问题探讨
一.前言 继上一篇写完字节编码内容后,现在分析在Java中各字符编码的问题,并且由这个问题,也引出了一个更有意思的问题,笔者也还没有找到这个问题的答案.也希望各位园友指点指点. 二.Java字符编码 ...
- spring-stutrs求解答
这里贴上applicationContext里的代码: <?xml version="1.0" encoding="UTF-8"?> <bea ...
- JavaScript Bind()趣味解答 包懂~~
首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...
- LeetCode Find the Celebrity
原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...
- CMMI4级实践中的5个经典问题及解答
这五个问题相当经典而且比较深,需要做过CMMI4.5级的朋友才能看懂这些问题.这5个问题是一位正在实践CMMI4级的朋友提出来的,而解答则是我的个人见解. 五个疑问是: A.流程,子流程部分不明白 ...
随机推荐
- Codeforces Round #272 (Div. 1) Problem C. Dreamoon and Strings
C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Putty server refused our key的解决方法
在使用putty工具使用密钥远程登陆CentOS系统时,出现Putty server refused our key提示,解决办法: 1.查看是否关掉SELINUX. 相关命令:getenforce, ...
- hdu 5410 CRB and His Birthday(混合背包)
Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...
- 程序员的绘图利器 — Gnuplot
介绍 Gnuplot is a command-line program that can generate two- and three-dimensional plots. It is fre ...
- gulp 构建工具
1. gulp 的简介 gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以 ...
- Javascript基础引用类型之Object
虽然说ECMAScript也是一门对象语言,但是它和其他面向对象语言还是有区别的,它不具有类和接口等基本结构.所以在ECMAScript中一般说类指的是引用类型.创建Object实例的方式有两种: 第 ...
- angularJS自定义过滤器、服务和指令
自定义过滤器 mainApp.filter('mayfilter',function(){ return function(input){ (过滤逻辑代码) } }); 自定义创建指令 mainA ...
- FineUI上传控件
文件上传 现在就简单多了,并且也漂亮多了,参考这个示例. 1: <ext:SimpleForm ID="SimpleForm1" BodyPadding="5px& ...
- JDBC 异常特殊原因 (数据库只读解决办法)
JDBC 异常特殊原因 有时候并不是因为程序写的有问题 ,是因为 数据库只读 在sqlserver2005中附加数据库时,附加的数据库会变成只读的,只能进行查询操作. 解决方法: 1 打开Sq ...
- hdu5351 MZL's Border(规律题,java)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud MZL's Border Time Limit: 2000/1000 MS (Ja ...