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

/* 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 res = 0;
// find the celebrity
for (int i = 0; i < n; i++) {
if (knows(res, i)) {
res = i;
}
} // return -1 if res is not celebrity
for (int i = 0; i < n; i++) {
if (i != res && (knows(res, i) || !knows(i, res))) {
return -1;
}
}
return res;
}
}

[LC] 277. Find the Celebrity的更多相关文章

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

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

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

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

  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. <Array> 277 243 244 245

    277. Find the Celebrity knows(i, j): By comparing a pair(i, j), we are able to discard one of them 1 ...

随机推荐

  1. STM32F407读编码器没上拉电阻遇见的问题

    在调试之前由于本科阶段参加飞思卡尔智能汽车的竞赛,一直在使用与竞赛相关的单片机和编码器,后来由于工程的需要开始使用STM32的板子,在调试编码器的时候遇见了,使用了STM32的官方标准库中的定时器正交 ...

  2. jsp的appilication.getInitParameter()方法无法获取到值的问题

    背景介绍 今天研究jsp的内置对象时发现,使用appilication.getInitParameter()从web.xml文件中获取值的时候,死活获取不到,折腾了将近一个小时,后来出现问题的原因却让 ...

  3. 关于js返回上一页的实现方法

    以前在提交表单的时候,如果提交出错返回的时候信息内容全没了,我不知道要怎么保存,就开始了那种最愚蠢的做法,将填写的数据设置到session中,让后取出来用,不过没有试成功,总是有错,无意之中在我那本j ...

  4. 201503-1 图像旋转 Java

    思路: 观察输入和输出可以发现,第三列输出为第一行,第二列输出为第二行,第一列输出为第三行.循环即可 import java.util.Scanner; //得分80,本题最高需要输入100W次,因为 ...

  5. PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]

    题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...

  6. sublime text2设置快捷键打开浏览器

    1 编辑一个py文件,内容如下: import sublime, sublime_plugin import webbrowser url_map = { 'C:\\server\\www\\' : ...

  7. Linux-waitpid介绍

    1.waitpid与wait差别 (1).基本功能是一样的,都是用来回收子进程 (2).waitpid可以回收指定PID的子进程 (3).waitpid可以阻塞式或非阻塞式两种工作模式 2.代码示例 ...

  8. UI Automation编程辅助工具Inspect的下载和使用

    UIAutomation微软提供的UI自动化库,主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素,.NET Windows的窗体应用程序和WPF应用程序. In ...

  9. Python笔记_第四篇_高阶编程_py2与py3的区别

    1. 性能: py3.x起始比py2.x效率低,但是py3.x现有极大的优化空间,效率正在追赶. 2. 编码: py3.x原码文件默认使用的utf-8编码,使得变量名更为宽阔. 3. 语法: * 去除 ...

  10. Python笔记_第一篇_面向过程_第一部分_9.Ubuntu基础操作

    第一部分   Ubuntu简介 Ubuntu(乌班图)是一个机遇Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“Ubuntu”一词,意思是“人性”.“我的存在 ...