原题链接在这里: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. iOS 编码转换

    - (NSString *)SaveFileToDocuments:(NSString *)url { // NSString *url = @"http://172.28.250.70/a ...

  2. Handlebars块级Helpers

    1.Handlebars简单介绍: Handlebars是JavaScript一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less templat ...

  3. Devexpress使用经验1

    1. 使用RibbonForm时,修改左上角图标:添加ApplicationMenu1,在属性中找Ribbon->ApplicationIcon,设置图标 2. 隐藏DevExpress Xtr ...

  4. Vue#组件

    组件: 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能. 使用: ...

  5. .NET Remoting 体系结构 之 在 ASP.NET 中驻留远程服务器

    迄今为止,所有服务器示例都是运行在自驻留(self-hosted)的.NET 服务器上.自驻留的服务器必 须手动启动..NET Remoting 服务器也可以在许多其他的应用程序类型中启动.在 Win ...

  6. form-line 样式 让 两个控件在同一个水平位置

    <div class="row"> <div> <label class="form-inline">参加单位:<in ...

  7. 【转】Oracle索引列NULL值引发执行计划该表的测试示例

    有时开发进行表结构设计,对表字段是否为空过于随意,出现诸如id1=id2,如果允许字段为空,因为Oracle中空值并不等于空值,有可能得到意料之外的结果.除此之外,最关键的是,NULL会影响oracl ...

  8. NOI 题库 7218

    7218  献给阿尔吉侬的花束 描述 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜 ...

  9. 【JAVA】JDK -Calendar 遇到的 一个坑

    Calendar是JDK 1.1增加的类 最近使用了下Calendar发现几个很让人抓狂的问题 源码: public final static int SUNDAY = 1; public final ...

  10. [canvas]用canvas绘制饼状图

    折线图之后又来饼状图啦~\(≧▽≦)/~啦啦啦 <!DOCTYPE html> <html lang="en"> <head> <meta ...