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 - 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.
链接: 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 ...
随机推荐
- Liferay IDE 3.1 M1发布啦
很嗨森,以后就再也不用SDK和下载.ivy啦 新增功能主要有: 1.Liferay Workspace(用来存放Liferay Module项目) 2. Liferay Gradle Module P ...
- The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit
如果你是通过搜索来到本文的,相信你应该是遇到了如下的错误 The code of method _jspService(HttpServletRequest, HttpServletResponse) ...
- [转]Similarities between Hibernate and JDBC objects
- 1566: [NOI2009]管道取珠 - BZOJ
Description Input第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. 第三行 ...
- 【BZOJ】【1042】【HAOI2008】硬币购物
DP+容斥原理 sigh……就差一点…… 四种硬币的数量限制就是四个条件,满足条件1的方案集合为A,满足条件2的方案集合为B……我们要求的就是同时满足四个条件的方案集合$A\bigcap B\bigc ...
- Leetcode#61 Rotate List
原题地址 我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊.比如: 1->2->3->4->5->NULL 向右旋转2的距离,变成了 ...
- vsm shadowmap format
遇到个奇怪的问题. 在做vsm ,shadowmap format RGBA8 结果正常 RGBA16F 场景不形成阴影的地方变纯黑,因为sm里面这些地方变纯黑(感觉这个好修一些) RGBA32F 阴 ...
- .net程序集强名称签名实践
引用: http://www.cnblogs.com/cpcpc/archive/2011/01/17/2123086.html 强名称是由程序集的标识加上公钥和数字签名组成的.其中,程序集的标识包 ...
- Oracle 显示时间问题
在部署的时候. 显示的时间为会 2014/1/1 9:00:00 pm 但开发过程中显示为正常: 2014-1-1 21:00:00 解决方法: 1. Oracle数据库的时间格式没有问题, ...
- js获取,设置FCKeditor内容
// 获取编辑器中HTML内容 function getEditorHTMLContents(EditorName) { var oEditor = FCKeditorAPI.GetInsta ...