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 - 1people 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.流程,子流程部分不明白 ...
随机推荐
- [IOS]包含增删改查移动的tableView展示+plist文件保存+程序意外退出保存Demo
做一个tableView,包含增删改移动功能,并且修改值的时候,在按home键的时候会自动保存.如果可以的话使者保存自定义的类数组保存到plist中. 实现步骤: 1.创建一个SingleViewAp ...
- js 获取input file路径改变图像地址
html代码 <img id="newImage" alt="100x100" src="__PUBLIC__/img/1.jpg" ...
- Linux文件io--one简述
什么是文件IO IO就是input/output,输入/输出.文件IO的意思就是读写文件. 文件操作的主要接口API 什么是操作系统API (1)API是一些函数,这些函数是由linux系统提供支持的 ...
- 第18讲- UI常用组件之EditText
第18讲UI常用组件之EditText 三.文本输入框EditText EditTex类继承自TextView.EditText是接受用户输入信息的最重要控件.在html当中,相当于<input ...
- Spring 的优秀工具类盘点第 2 部分
特殊字符转义 由于 Web 应用程序需要联合使用到多种语言,每种语言都包含一些特殊的字符,对于动态语言或标签式的语言而言,如果需要动态构造语言的内容时,一个我们经常会碰到的问题就是特殊字符转义的问题. ...
- eclipse 集成maven插件
本文转载自:http://www.blogjava.net/fancydeepin/archive/2012/07/13/eclipse_maven3_plugin.html 环境准备: eclips ...
- Internetmap.apk实现原理分析
1.本地实现调用 程序根据data文件目录下的asinfo.json文件(包含自治域网络名和对应的坐标值),调用so文件绘制asn结点图(ASN,AutoSystemNode,自治域结点) 2.路由查 ...
- CentOS CVS安装使用
CentOS CVS安装使用 一.CVS简介 CVS(Concurrent Versions System)版本控制系统:是一种GNU软件包,CVS是一个C/S系统,主要用于在多人开发环境下的源码 ...
- highcharts的使用
步骤: 1. 去highcharts官网下载最新版本 2. 在.aspx页面添加引用 例: <link href="../JS/highcharts/css/highslide.css ...
- js、java传值编码
一.请求使用post方法不需要考虑编码问题.二.前后台统一编码,支持中文,不考虑编码:tomcat utf-8编码三.前后台编码不统一 $.ajax({ url : &q ...