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 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
.
链接: http://leetcode.com/problems/find-the-celebrity/
题解:
在数组中找Celebrity。Celebrity的条件很特殊,首先所有人都认识Celebrity,其次Celebrity不认识任何人。一开始想的方法是Brute force,需要O(n2)遍历。看了Discuss以后发现我们需要好好利用第一个条件,就是假定candidate = 0,遍历数组,当knows(candidate, i)时, 假定i为candidate。这里因为所有人都认识Celebrity,所以遍历完数组以后candidate就是我们要验证的Celebrity。但也有还需要第二遍遍历来验证这个candidate是否满足我们的条件。
Time Complexity - O(n), Space Complexity - O(1)
/* 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;
for(int i = 1; i < n; i++) {
if(knows(candidate, i)) {
candidate = i;
}
} for(int i = 0; i < n; i++) {
if(i != candidate && (knows(candidate, i) || !knows(i, candidate))) {
return -1;
}
} return candidate;
}
}
二刷:
还是和一刷方法一样。先假定candidate = 0,从1开始遍历数组,假如knows(candidate, i),则我们把candidate更新为i。第一次遍历完毕以后,我们再开始第二次遍历,来判断是否candidate确实为我们的解。
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 candidate = 0;
for (int i = 1; i < n; i++) {
if (knows(candidate, i)) candidate = i;
}
for (int i = 0; i < n; i++) {
if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) return -1;
}
return candidate;
}
}
Reference:
https://leetcode.com/discuss/56350/straight-forward-c-solution-with-explaination
https://leetcode.com/discuss/56413/java-solution-two-pass
https://leetcode.com/discuss/61140/java-python-o-n-calls-o-1-space-easy-to-understand-solution
https://leetcode.com/discuss/56349/python-brute-force-solution-easy-understanding
277. Find the Celebrity的更多相关文章
- 名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)
1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- [LC] 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 ...
- 【LeetCode】277. Find the Celebrity 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- [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 ...
- <Array> 277 243 244 245
277. Find the Celebrity knows(i, j): By comparing a pair(i, j), we are able to discard one of them 1 ...
随机推荐
- C++中的static关键字(转)
原出处:http://blog.csdn.net/hackbuteer1/article/details/7487694 C++的static有两种用法:面向过程程序设计中的static和面向对象程序 ...
- Asp.Net MVC3(三)-MvcApp实现全局异常捕获
定义异常捕获类: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMu ...
- 玩耍Hibernate之缓存
2.在持久化层,对象分为哪些状态?分别列出来. 答:瞬时态(Transient).持久态(Persistent).脱管态(Detached). 瞬时态(Transient) 是对象是创建时,瞬时对象在 ...
- Window 添加定时任务
简单任务 右键点击 我的电脑->管理->任务计划程序库->创建基本任务 然后选择任务类型,触发时间,触发程序就可以了,可以精确到秒 带参数的计划任务 如果执行程序是cmd 可选参数那 ...
- [转]Similarities between Hibernate and JDBC objects
- 【POJ】【1637】Sightseeing tour
网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...
- spring framework项目源码github托管地址
方法一:直接下载,github托管地址:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/ 方法二: ...
- 项目前端技术-learn
赶鸭子上架 之 前端学习 目前项目中的前端用到的技术主要是:1. bootstrap框架; 2. 基于javscript的jQuery, jQuery ui; 3. dust前端模板引型.
- shader 里面的分支
shader 里面的真分支会降低效率 一种方法:构造一个分段函数出来 比如saturate(depth*1.5f)
- 网络(一),libevent客户端部分
网络模块() 一.服务端: 暂时就以libevent模块,共享内存等下 .GS打开,首先创建4个libevent子线程,当然为每个线程设置连接通知回调函数,这个是基于sockpair的,然后再创建一个 ...