[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 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 n
matrix wherea[i][j] = 1
means personi
knows personj
whilea[i][j] = 0
means 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] 277. 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]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 ...
- 名人问题 算法解析与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] 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
Problem: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...
- Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)
Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number) 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 ...
- Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)
Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target) 给定一个只包含小写字母的有序数组letters ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
随机推荐
- 微信小程序开发语音识别文字教程
微信小程序开发语音识别文字教程 现在后台 添加插件 微信同声传译 然后app.json 加入插件 "plugins": { "WechatSI": { &quo ...
- Map映射如何使用迭代器?
迭代器只针对集合类型的数据,因此map类型的必须先转换成集合类型才能使用迭代器去获取元素. 1.在map中虽然不能直接实例化迭代器,但map集合提供了keySet()方法和value()方法,可以通过 ...
- vue中toggle切换的3种写法
前言:查看下面代码,在任意编辑器中直接复制粘贴运行即可 1:非动态组件(全局注册2个组件,借用v-if指令和三元表达式) <!DOCTYPE html> <html> < ...
- jQuery 源码解析(三) pushStack方法 详解
该函数用于创建一个新的jQuery对象,然后将一个DOM元素集合加入到jQuery栈中,最后返回该jQuery对象,有三个参数,如下: elems Array类型 将要压入 jQuery 栈的数组元素 ...
- Vue.js 源码分析(二) 基础篇 全局配置
Vue.config是一个对象,包含Vue的全局配置,可以在启动应用之前修改下列属性,如下: ptionMergeStrategies ;自定义合并策略的选项silent ...
- Django学习笔记(15)——中间件
当Django处理一个Request的过程是首先通过中间件,然后再通过默认的URL方式进行的.我们可以在Middleware这个地方把所有Request拦截住,用我们自己的方式完成处理以后直接返回Re ...
- Knative 实践:从源代码到服务的自动化部署
通过之前的文章,相信大家已经熟悉了 Serving.Eventing 以及 Tekton.那么在实际使用中,我们往往会遇到一些复杂的场景,这时候就需要各个组件之间进行协作处理.例如我们提交源代码之后是 ...
- 关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!
https://www.cnblogs.com/PurpleTide/archive/2010/11/25/1887506.html CLR via C# 读书笔记 2-3 Cache Lines a ...
- ASP.NET MVC IOC 之 Autofac 系列开篇
本系列主要讲述Autofac在.NET MVC项目以及webform中的使用. autofac为IOC组件,实现控制反转,主要结合面向接口编程,完成较大程度的解耦工作. 作为初学者,将学习到的每一步, ...
- Django基本知识
一.安装及使用 下载安装 命令行:pip3 install django==1.11.21 pycharm 创建项目 命令行: 找一个文件夹存放项目文件,打开终端: django-admin star ...