277. Find the Celebrity
2015-12-10 05:37
344 查看
题目:
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
链接: 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)
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
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.
链接: 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; } }
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
相关文章推荐
- 调色板类QPalette——包含了Qt窗口不见的颜色组(collor group),和Windows右键属性外观非常类似
- Windows 系统消息范围和前缀,以及消息大全
- SVM在R语言中的应用
- Qt 不规则窗体的实现(构造函数里setPaletteBackgroundPixmap后设置setMask)
- 【Html】Relative与Absolute组合使用
- VC++中的类的内存分布(上)(通过强制转换,观察地址,以及地址里的值来判断)
- 【Html】层模型--固定定位
- 【Html】层模型--相对定位
- hdu 1698 线段树区间修改
- Android RecyclerView使用详解三
- 【Html】层模型--绝对定位
- 【Html】什么是层模型?
- 【Html】浮动模型
- 加黑部分 执行的时候报 IO异常 球大神解答
- C++专题(一)
- 【Html】流动模型(二)
- Eclipse报错 Please ensure that adb is correctly located at '.\adb.exe' and can be
- ObjectAnimator动画使用
- 【Html】流动模型(一)
- 【Html】css布局模型