原题链接在这里:https://leetcode.com/problems/find-the-celebrity/

题目:

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.

题解:

先找一个candidate. 若是celebrity 认识 i, 说明i 有可能是celebrity. 就更新i为candidate.

找到这个candidate 后 再扫一遍来判定这是不是一个合格的candidate, 若是出现candidate认识i 或者 i不认识candidate的情况, 说明这不是一个合格的candidate.

Time Complexity: O(n). Space: O(1).

AC 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 celebrity = 0;
//找一个candidate
for(int i = 0; i<n; i++){
if(knows(celebrity, i)){
celebrity = i;
}
}
for(int i = 0; i<n; i++){
//若是出现candidate认识i 或者 i不认识candidate的情况, 说明这不是一个合格的candidate
if(i != celebrity && (knows(celebrity, i) || !knows(i, celebrity))){
return -1;
}
}
return celebrity;
}
}

类似Find the Town Judge.

LeetCode Find the Celebrity的更多相关文章

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

  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]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 解题报告 (C++)

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

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

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

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. Linux常用命令学习8---(用户和用户组管理)

    1.用户和用户组     用户和用户组概念        用户:使用操作系统的人(Linux支持多个用户在同一时间登陆同一个操作系统)        用户组:具有相同权限的一组用户(Linux系统中可 ...

  2. Power BI FAQ

    关于Power BI,最近一直想写点什么,但是也想不到写什么.直到前几天,有个朋友在qq加我,问了我好几个问题,我发现都是一些很有代表性的问题,所以都记录了下来,特意整理了一下分享出来,希望能帮到更多 ...

  3. 几种服务器端IO模型的简单介绍及实现

    一些概念: 同步和异步 同步和异步是针对应用程序和内核的交互而言的,同步指的是用户进程触发I/O操作并等待或者轮询的去查看I/O操作是否就绪,而异步是指用户进程触发I/O操作以后便开始做自己的事情,而 ...

  4. POJ2104 K-th Number(归并树)

    平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...

  5. 享元模式/Flyweight模式/对象结构型/设计模式

    flyweight 享元模式(对象结构型) Flyweight在拳击比赛中指最轻量级,即"蝇量级"或"雨量级",这里选择使用"享元模式"的意 ...

  6. 三言两语之微信小程序开发初体验(1)

    一.前情   直接切入主题,微信发布了小程序,前端开发者表示,如果不会微信小程序的开发感觉就跟不上时代了,先解答几个容易出现歧义的问题 小程序就叫小程序,不叫应用号,因为apple不准,哈哈 小程序是 ...

  7. 如何直接在ftp里编辑文件

    首先要连接ftp,如何链接ftp,这个我已经分享过,如果还有不懂的,可以查看下经验分享,打开ftp,并连接你要修改的站点!   下载安装代码编辑器,比如EditPlus.sublime text等,随 ...

  8. Excel大批量导入数据到SQLServer数据库-万条只用1秒

    private string ExcelToStudent() { /*---*/ var preStr = DateTime.Now.ToString("yyyyMMddHHmmssfff ...

  9. Hibernate学习笔记3

    ---恢复内容开始--- 一.hibernate如何转化jdbc代码实例[通过hibernate构建jdbc后往数据库传对象] import java.sql.Connection;import ja ...

  10. DB Connection String

    SQL: Data Source=APGZDB04;Initial Catalog=ChinaEDI;Persist Security Info=True;User ID=edi_ac;Passwor ...