277. Find the Celebrity

时间:2022-07-27 09:02:18

题目:

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的更多相关文章

  1. 名人问题 算法解析与Python 实现 O&lpar;n&rpar; 复杂度 (以Leetcode 277&period; Find the Celebrity为例)

    1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...

  2. &lbrack;LeetCode&rsqb; 277&period; 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 ...

  3. &lbrack;LeetCode&num;277&rsqb; Find the Celebrity

    Problem: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...

  4. LeetCode 277&period; Find the Celebrity (找到明星)&dollar;

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  5. &lbrack;leetcode&rsqb;277&period; 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. &lbrack;LC&rsqb; 277&period; 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&period; Find the Celebrity 解题报告 &lpar;C&plus;&plus;&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  8. &lbrack;leetcode&rsqb;277&period; 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 ...

  9. &lt&semi;Array&gt&semi; 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 ...

随机推荐

  1. Socket编程基础知识

    端口号常识:  端口号被从1 开始分配.    通常端口号超出255 的部分被本地主机保留为私有用途.    1到255 之间的号码被用于远程应用程序所请求的进程和网络服务.    每个网络通信循环地 ...

  2. JavaGUI——设置框架背景颜色和按钮颜色

    import java.awt.Color; import javax.swing.*; public class MyDraw { public static void main(String[] ...

  3. android 手电筒的实现

    android手机用闪光灯做成手电筒的应用非常多,可是有的不能用. 后来发现是除了把 camera device的 flashmode设置成torch外还要打开预览: 以下是代码: MainActiv ...

  4. 设计与实现的简单和经常使用的权限系统&lpar;五岁以下儿童&rpar;&colon;不维护节点的深度level,手工计算level,树形结构

     以这种方式.和第三的类似介绍.所不同的是.深度未在数据库中存储节点level,添加和更改时间,护.而是,在程序中,实时去计算的. 至于后面的,依照level升序排序,再迭代全部的节点构造树,与第三篇 ...

  5. mkpasswd 随机密码生成

    root@op-admin:~# mkpasswd -l -n usage: mkpasswd [args] [user] where arguments are: -l # (length of p ...

  6. 接口自动化框架&lpar;java&rpar;--5&period;通过testng&period;xml生成extentreport测试报告

    这套框架的报告是自己封装的 由于之前已经通过Extentreport插件实现了Testng的IReport接口,所以在testng.xml中使用listener标签并指向实现IReport接口的那个类 ...

  7. hdoj:2075

    A|B? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. VUE配置项结构

    VUE配置项结构 config:项目的配置文件 index.js: 基础的配置信息 dev.env.js:开发环境配置信息 prod.env.js:线上环境配置信息 build: 项目打包所需要的内容 ...

  9. linux常用指令集-持续更新&period;&period;&period;

    0.查看所有java进程GC情况:for i in `jps|egrep -v "Jps|Launcher" |cut -d" " -f1`;do pwdx $ ...

  10. O2O和B2C、C2C的区别

    B2C.C2C是在线支付,购买的商品会塞到箱子里通过物流公司送到你手中;O2O是在线支付,购买线下的商品.服务,再到线下去享受服务. O2O模式的核心很简单,就是把线上的消费者带到现实的商店中去.在线 ...