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), 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.

题意:

有一坨人,只允许你问“谁认识谁”。得想个法儿把名人给找出来。什么叫名人?谁都认识TA,TA谁也不认。

想起来牛姐有木有?

Solution:

只要call一次 knows(i, j) by comparing a pair (i, j),  we are able to discard one of them

1.Find a candidate by one pass: make sure other people are not celebrities.if knows(i, j) is true,  i is guaranteed not to be celebrityotherwise, j is not a celebrity

2.Make sure if the candidate is the celebrity by one pass

code

 /* 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;
// assume candidate is celebrity
// if candidate knows i, such candidate isn't celebrity, try another one
for(int i = 1; i < n; i++){
if(knows(candidate, i))
candidate = i;
} for(int i = 0; i < n; i++){
// we don't want to check if person knows himself
// if candidate knows i, such candidate isn't celebrity
// if nobody knows candidate, such candidate isn't celebrity
if(candidate!=i && (knows(candidate, i) || !knows(i, candidate))) return -1;
}
return candidate;
}
}

[leetcode]277. 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]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 ...

  3. 名人问题 算法解析与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 ...

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

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

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

  7. 【LeetCode】277. Find the Celebrity 解题报告 (C++)

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

  8. 277. Find the Celebrity

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

  9. [LeetCode] Lemonade Change 买柠檬找零

    At a lemonade stand, each lemonade costs $5.  Customers are standing in a queue to buy from you, and ...

随机推荐

  1. 关于input=file的用法

    <input type="file"/>这个东西是用来上传图片用的. 1,但是存在一下问题但是在在各个浏览器下的显示是不一样的 IE下: IE之外的浏览器: 2.如果不 ...

  2. Web 过滤器参数设置问题

    问题描述: 在代码定义了3个过滤器,分别为filter1,filter2,filter3,过滤的Servlet范围分别是"/*","/Servlet1",&qu ...

  3. 代码生成器 CodeSmith 的使用(六)

    在上一篇的版本中,我们生成了数据库所有表中的字段,如果要使数据库中的单个表 生成 PetaPoco 构架下的 ORM 映射,使那怎么办.这是这篇博客的主要内容. 首先来看完整的 Camel 规则模板: ...

  4. License分类 + 引入开源软件时License的注意事项

    License分类 GPL: linux.openJDK,openJFX,mysql 融合感染,单独子模块不感染(自己的模块与引入模块的通信方式是socket) openJDK(GNU General ...

  5. YUM软件包额外扩展了解项

    5.YUM配置文件 yum的配置一般有两种方式: 一种是直接配置/etc目录下的yum.conf文件, 另外一种是在/etc/yum.repos.d目录下增加.repo文件 [root@xuliang ...

  6. Some facts about topological sort

    Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j. ...

  7. java的锁机制,synchronize与Lock比较

    参考:https://blog.csdn.net/dahongwudi/article/details/78201082

  8. JAVA SpringMVC + FormDate + Vue + file表单 ( 实现 js 单文件和多文件上传 )

    JS 部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  9. 一个js程序:离下一个圣诞节还有多少天?

    话不多说上代码: //离下一个圣诞节还有多少天 var christ=new Date(); christ.setMonth(11); christ.setDate(25); var year=now ...

  10. How to Pronounce WH Words — what, why, which

    How to Pronounce WH Words — what, why, which Share Tweet Share Have you noticed that there are two d ...