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:

  1. The directed graph is represented as an adjacency matrix, which is an n x n matrix where a[i][j] = 1 means person i knows person j while a[i][j] = 0 means the contrary.
  2. 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

https://leetcode.com/problems/find-the-celebrity/discuss/71228/JavaPython-O(n)-calls-O(1)-space-easy-to-understand-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Find the Celebrity 寻找名人的更多相关文章

  1. [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 ...

  2. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

  3. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  4. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素2

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素2 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. LeetCode Find the Celebrity

    原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...

  9. [LeetCode] Find Duplicate Subtrees 寻找重复树

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

随机推荐

  1. java笔记--理解java类加载器以及ClassLoader类

    类加载器概述: java类的加载是由虚拟机来完成的,虚拟机把描述类的Class文件加载到内存,并对数据进行校验,解析和初始化,最终形成能被java虚拟机直接使用的java类型,这就是虚拟机的类加载机制 ...

  2. go语言注释

    Go语言注释实例代码教程 - Go支持C语言风格的/* */块注释,也支持C++风格的//行注释. 当然,行注释更通用,块注释主要用于针对包的详细说明或者屏蔽大块的代码. 每个包都应有一个包注解,即 ...

  3. 完美解决,浏览器下拉显示网址问题 | 完美解决,使用原生 scroll 写下拉刷新

    在 web 开发过程中我们经常遇到,不想让用户下拉看到我的地址,也有时候在 div 中没有惯性滚动,就此也出了 iScroll 这种关于滚动条的框架,但是就为了一个体验去使用一个框架好像又不值得,今天 ...

  4. Navisworks API 简单二次开发 (自定义工具条)

    在Navisworks软件运行的时候界面右侧有个工具条.比较方便.但是在二次开发的时候我不知道在Api那里调用.如果有网友知道请告诉我.谢谢. 我用就自己设置一个工具.界面比较丑!没有美工. 代码: ...

  5. 数据上下文【 DnContext】【EF基础系列7】

    DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...

  6. Android ORM -- Litepal(2)

    4. 更新数据 ContentValues value = new ContentValues(); value.put("name", "计算机网络2"); ...

  7. Matlab 之 find()函数

    当我第一次用matlab语言编写一个工程项目时,发现自己编写的脚本里循环特别多,导致编程效率很低,这让我特别苦恼.有一次导师让我阅读他编写的一个Matlab脚本,并按照新要求对其进行更改.我发现脚本里 ...

  8. MVC Api 的跨项目路由

    现有Momoda.Api项目,由于团队所有人在此项目下开发,导致耦合度太高,现从此接口项目中拆分出多个子项目从而避免对Momda.Api的改动导致“爆炸” MVCApi的跨项目路由和MVC有解决方式有 ...

  9. Struts2入门(四)——数据输入验证

    一.前言 1.1.什么是输入验证?为什么需要输入验证? 在上一篇文章中,我们学习了数据类型转换,我们提到了表示层数据处理的两个方法,也提到了用户输入数据需要进行类型转换才能得到我们想要的数据,那么,我 ...

  10. SharePoint 部署时报错: 未能提取此解决方案中的cab文件

    在vs里右击SharePoint项目,选择"部署",结果报错: Error occurred in deployment step 'Add Solution':Fail to e ...