LeetCode Find the Celebrity
2016-04-01 09:11
344 查看
原题链接在这里:https://leetcode.com/problems/find-the-celebrity/
题目:
Suppose you are at a party with
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
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
题解:
先找一个candidate. 若是celebrity 认识 i, 说明i 有可能是celebrity. 就更新i为candidate.
找到这个candidate 后 再扫一遍来判定这是不是一个合格的candidate, 若是出现candidate认识i 或者 i不认识candidate的情况, 说明这不是一个合格的candidate.
Time Complexity: O(n). Space: O(1).
AC Java:
题目:
Suppose you are at a party with
npeople (labeled from
0to
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.
题解:
先找一个candidate. 若是celebrity 认识 i, 说明i 有可能是celebrity. 就更新i为candidate.
找到这个candidate 后 再扫一遍来判定这是不是一个合格的candidate, 若是出现candidate认识i 或者 i不认识candidate的情况, 说明这不是一个合格的candidate.
Time Complexity: O(n). Space: O(1).
AC Java:
/* 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) { if(n <= 1){ return -1; } int celebrity = 0; //找一个candidate for(int i = 0; i<n; i++){ if(knows(celebrity, i)){ celebrity = i; } } for(int i = 0; i<n; i++){ //若是出现candidate认识i 或者 i不认识candidate的情况, 说明这不是一个合格的candidate if(i != celebrity && (knows(celebrity, i) || !knows(i, celebrity))){ return -1; } } return celebrity; } }
相关文章推荐
- 将博客搬至博客园
- Hello, World!
- 对称密钥与非对称密钥
- Vim 程序编辑器 经常使用操作
- js+flash实现的5图变换效果广告代码(附演示与demo源码下载)
- 51单片机开发的几点心得
- 51单片机开发的几点心得
- 51单片机开发的几点心得
- maven使用总结
- bash逐行读取文件内容
- HTML5培训第二节课笔记(事件,原型,JSON)
- 通过CDC获取 HDC
- javax.naming.NoInitialContextException: Need to specify class name in environment or system property
- ubuntu windows 双系统引导配置
- Android BroadcastReceiver学习总结
- 【poj 3258】River Hopscotch 题意&题解&代码(C++)
- 【代码笔记】检测手机翻转
- java泛型
- oracle的SQL语句中的(+)是干什么用的?
- angularJs中上传图片/文件功能:ng-file-upload