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. TACACS+简单说明

    1 TACACS+概述 1.1 什么是TACACS+ TACACS+(Terminal Access Controller Access Control System,终端访问控制器控制系统协议)是在 ...

  2. 新手搭建 nginx + php (LNMP)

    配置源 纯净的Centos 6.5系统 配置163yum源 (这个比较简单,百度能解决很多问题) 开始 安装  开发软件包:yum  -y groupinstall  "Developmen ...

  3. Java 类的生命周期

    类从被加载到JVM内存中开始,到卸载出内存为止,它的整个生命周期包括: 加载(Loading)-->验证(Verification)-->准备(Preparation)-->解析(R ...

  4. spring的Ioc容器与AOP机制

    为什么要使用Spring的Ioc容器? 1.首先,spring是一个框架,框架存在的目的就是给我们的编程提供简洁的接口,可以使得我们专注于业务的开发,模块化,代码简洁,修改方便. 通过使用spring ...

  5. 学习MongoDB 四: MongoDB查询(一)

    一.简介 MongoDB提供了db.collection.find() 方法可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段. 二.db.collection.fi ...

  6. mongodb中查询返回指定字段

    mongodb中查询返回指定字段   在写vue项目调用接口获取数据的时候,比如新闻列表页我只需要显示新闻标题和发表时间,点击每条新闻进入详情页的时候才会需要摘要.新闻内容等关于此条新闻的所有字段.  ...

  7. Redis安装测试

    1.在线下载,redis是C语言开发的,编译需要依赖一个gcc的环境: yum install gcc-c++,需要保证网络畅通,在线安装: 键入y 环境安装完成后,接下来安装redis; 可以先去h ...

  8. Django之模板Template

    模板介绍 作为Web框架,Django提供了模板,可以很便利的动态生成HTML 模版系统致力于表达外观,而不是程序逻辑 模板的设计实现了业务逻辑(view)与显示内容(template)的分离,一个视 ...

  9. text-overflow:ellipsis ,溢出文本显示省略号

    text-overflow:ellipsis 对溢出文本显示省略号有两个好处, 一是不用通过程序限定字数 二是有利于SEO. 需要使用对对溢出文本显示省略号的通常是文章标题列表,这样处理对搜索引擎更友 ...

  10. JavaScript判断浏览器及其版本信息

    通过window.navigator来判断: function getBrowserInfo(){ var Info = {}; var str = window.navigator.userAgen ...