[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 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). 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.
Example 1:
Input: graph = [
[1,1,0],
[0,1,0],
[1,1,1]
]
Output: 1
Explanation: There are three persons labeled with 0, 1 and 2. graph[i][j] = 1 means person i knows person j, otherwise graph[i][j] = 0 means person i does not know person j. The celebrity is the person labeled as 1 because both 0 and 2 know him but 1 does not know anybody.
Example 2:
Input: graph = [
[1,0,1],
[1,1,0],
[0,1,1]
]
Output: -1
Explanation: There is no celebrity.
Note:
- The directed graph is represented as an adjacency matrix, which is an 
n x nmatrix wherea[i][j] = 1means personiknows personjwhilea[i][j] = 0means the contrary. - Remember that you won't have direct access to the adjacency matrix.
 
这道题让我们在一群人中寻找名人,所谓名人就是每个人都认识他,他却不认识任何人,限定了只有1个或0个名人,给定了一个 API 函数,输入a和b,用来判断a是否认识b,让我们尽可能少的调用这个函数,来找出人群中的名人。博主最先想的方法是建立个一维数组用来标记每个人的名人候选状态,开始均初始化为 true,表示每个人都是名人候选人,然后一个人一个人的验证其是否为名人,对于候选者i,遍历所有其他人j,如果i认识j,或者j不认识i,说明i不可能是名人,那么标记其为 false,然后验证下一个候选者,反之如果i不认识j,或者j认识i,说明j不可能是名人,标记之。对于每个候选者i,如果遍历了一圈而其候选者状态仍为 true,说明i就是名人,返回即可,如果遍历完所有人没有找到名人,返回 -1,参见代码如下:
解法一:
bool knows(int a, int b);
class Solution {
public:
int findCelebrity(int n) {
vector<bool> candidate(n, true);
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
if (candidate[i] && i != j) {
if (knows(i, j) || !knows(j, i)) {
candidate[i] = false;
break;
} else {
candidate[j] = false;
}
}
}
if (candidate[i]) return i;
}
return -;
}
};
我们其实可以不用一维数组来标记每个人的状态,对于不是名人的i,直接 break,继续检查下一个,但是由于没有标记后面的候选人的状态,所以有可能会重复调用一些 knows 函数,所以下面这种方法虽然省了空间,但是调用 knows 函数的次数可能会比上面的方法次数要多,参见代码如下:
解法二:
bool knows(int a, int b);
class Solution {
public:
int findCelebrity(int n) {
for (int i = , j = ; i < n; ++i) {
for (j = ; j < n; ++j) {
if (i != j && (knows(i, j) || !knows(j, i))) break;
}
if (j == n) return i;
}
return -;
}
};
下面这种方法是网上比较流行的一种方法,设定候选人 res 为0,原理是先遍历一遍,对于遍历到的人i,若候选人 res 认识i,则将候选人 res 设为i,完成一遍遍历后,来检测候选人 res 是否真正是名人,如果判断不是名人,则返回 -1,如果并没有冲突,返回 res,参见代码如下:
解法三:
bool knows(int a, int b);
class Solution {
public:
int findCelebrity(int n) {
int res = ;
for (int i = ; i < n; ++i) {
if (knows(res, i)) res = i;
}
for (int i = ; i < n; ++i) {
if (res != i && (knows(res, i) || !knows(i, res))) return -;
}
return res;
}
};
由热心网友 fgvlty 提醒,还可以进一步减少 API 的调用量,找候选者的方法跟上面相同,但是在验证的时候,分为两段,先验证候选者前面的所有人,若候选者认识任何人,或者任何人不认识候选者,直接返回 -1。再验证候选者后面的人,这时候只需要验证是否有人不认识候选者就可以了,因为在最开始找候选者的时候就已经保证了候选者不会认识后面的任何人,参见代码如下:
解法四:
bool knows(int a, int b);
class Solution {
public:
int findCelebrity(int n) {
int res = ;
for (int i = ; i < n; ++i) {
if (knows(res, i)) res = i;
}
for (int i = ; i < res; ++i) {
if (knows(res, i) || !knows(i, res)) return -;
}
for (int i = res + ; i < n; ++i) {
if (!knows(i, res)) return -;
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/277
类似题目:
Find the Town Judge
参考资料:
https://leetcode.com/problems/find-the-celebrity/
https://leetcode.com/problems/find-the-celebrity/discuss/71227/Java-Solution.-Two-Pass
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Find the Celebrity 寻找名人的更多相关文章
- [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(4)寻找两个有序数组的中位数
		
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
 - 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素
		
[python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...
 - 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素2
		
[python]Leetcode每日一题-寻找旋转排序数组中的最小元素2 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...
 - [Swift]LeetCode277. 寻找名人 $ 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 ...
 - [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 Find the Celebrity
		
原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...
 - [LeetCode] Find Duplicate Subtrees 寻找重复树
		
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
 
随机推荐
- Bootstrap框架的学习(二)
			
一.下载Bootstrap Bootstrap (当前版本 v3.3.0)提供以下几种方式帮你快速上手,每一种方式针对具有不同技能等级的开发者和不同的使用场景. 下载地址:http://v3.boot ...
 - 挣值管理 EVM
			
项目的挣值管理(Earned Value Management,EVM),是用与进度计划.成本预算和实际成本相联系的三个独立的变量,进行项目绩效测量的一种方法. 有三个比较重要的参数,用这三个参数能够 ...
 - [示例] Firemonkey OnTouch 多点触控应用
			
说明:Firemonkey OnTouch 多点触控应用,可同时多指移动多个不同控件 原码下载:[原创]TestMultitouchMove_多点触控应用_by_Aone.zip 运行展示:
 - js 压缩工具总结
			
随便百度一个 “js 压缩”,然后就会有各种代码压缩工具可供选择, 向来我就爱那种绚丽新颖的玩意,比如这家显示压缩比呀,或者那家可以查错呀什么的, 今天还为国民浏览器拥有鼠标手势(按住右键画个图形就有 ...
 - 浅析天猫H5站点
			
前言 我们做前端开发的时候,很有可能会做一个竞品分析,比如我就做过去哪儿.艺龙.同程等与携程的移动站点竞品分析,竞品分析的目的一般是技术对比,但是更多的是业务对比,知己知彼,百战不殆:我们同时会借鉴. ...
 - kmdjs集成uglifyjs2打造极致的编程体验
			
回顾 上篇文章大概展示了kmdjs0.1.x时期的编程范式: 如下面所示,可以直接依赖注入到function里, kmdjs.define('main',['util.bom','app.Ball', ...
 - css_03之文本、浮动
			
1.字体属性:①字体格式:font-family:取值:“microsoft yahei”/Arial……:②字体大小:font-size:取值:pt/px:③字体加粗:font-weight:取值: ...
 - navigationController 去掉背景图片、去掉底部线条
			
//去掉背景图片 [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMe ...
 - SharePoint 2013 沙盒解决方案不能激活(激活按钮不可用)
			
把沙盒解决方案上传到目标站点的"解决方案"库中,发现"激活"按钮是灰掉的,不可用. 首先,我想到的是权限不足,所以 "以管理员身份"启动IE ...
 - HTML5之新增的元素
			
今天打开博客看到自己有了一个小粉丝,说实话还是蛮开心的,坚持写博客大半年了,终于迎来了自己的第一个小伙伴.总算是坚持了那么久的事情看到了结果吧. 前几天参加了我们学院老师的比赛---<青年教师大 ...