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. javascript创建对象之寄生构造函数模式(六)

    这种模式的基本思想是创建一个函数,该函数的作用仅仅是封装创建对象的代码,然后再返回新创建的对象; 但是从表面上看,这个函数有很像典型的构造函数. function createHuman(name,s ...

  2. html调用静态json例子

    1.json { "current": 2, "result": "success" } 1.html <!doctype html& ...

  3. socket编程一些注意的东西

    帮一个同学做了一下面试的作业.主要是socket编程要写一个多人博彩游戏室.没注意,被一些地方坑了一下,而且其实如果没有这个概念,还不好发现. 1.readLine() http://blog.csd ...

  4. Spring 配置 web.xml (防止spring 内存溢出)

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...

  5. leetcode506

    public class Solution { public string[] FindRelativeRanks(int[] nums) { var list = nums.OrderByDesce ...

  6. java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

    String[] 转换成 ArrayList 报的错. String[] str = {"A","B"}; ArrayList<String> li ...

  7. eclipse web run on server 404

    eclipse真是个坑爹玩意儿,前期在idea开发的web,移到eclipse遇到各种问题 刚开始好好的,突然404,不明所以,搞了好几天 参考eclipse修改web项目部署路径 解决了问题 后来发 ...

  8. 如何给echarts图表添加下载图表成图片的功能

    先打开一个现成的图表效果图,注意图中圈出的地方,如图   打开源码找到option,如图   在option下添加toolbox,如图   在toolbox下添加feature,如图   在featu ...

  9. eclipse 创建注释模板

    使用  Alt+Shift+J 可以快速注释. 我们每次手动敲入作者,时间,版本等信息,有一些重复,可通过设置eclipse注释模板,减少工作量. Window -> preference -& ...

  10. keepalive配置与管理

    什么是Keepalived呢,keepalived观其名可知,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用 ...