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. 转载:微信小程序源码提取反编译

    转载来源:www.51xuediannao.com/xiaochengxu/019c08cc.html 一.前言 微信小程序源码提取反编译,听起来很屌,其实还是简单的,基本是傻瓜式操作.要想拿到微信小 ...

  2. Python笔记_第一篇_面向过程_第一部分_2.内存详解

    Python的很多教材中并没有讲内存方面的知识,但是内存的知识非常重要,对于计算机工作原理和方便理解编程语言是非常重要的,尤其是小白,因此需要把这一方面加上,能够更加深入的理解编程语言.这里引用了C语 ...

  3. dfs--迷宫

    题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...

  4. block内存篇

    本文目的:对Block内存问题更加了解 概念理解:一种数据类型或比较特殊的对象,相当于一个指向函数的指针,该指针指向一段封装代码,调用block块代码好比调用该指针指向的函数代码 block分类: N ...

  5. ORB-SLAM2的编译运行以及TUM数据集测试

    ORB-SLAM2的编译运行以及TUM数据集测试 徐大徐 2018.02.06 17:04 字数 1838 阅读 2167评论 0喜欢 2 近段时间一直在学习高翔博士的<视觉SLAM十四讲> ...

  6. Django2.0——路由配置

    URL配置就像是Django所支撑网站的目录,它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表.URL需要在urls.py进行配置,与其对应的视图函数写在views.py文件中.ur ...

  7. cf1172E Nauuo and ODT(LCT)

    首先可以转化问题,变为对每种颜色分别考虑不含该颜色的简单路径条数.然后把不是当前颜色的点视为白色,是当前颜色的点视为黑色,显然路径数量是每个白色连通块大小的平方和,然后题目变为:黑白两色的树,单点翻转 ...

  8. 简单总结------redis

    一.Redis 是一个基于内存的高性能key-value数据库. 二.端口 6379 三.特点: Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在 ...

  9. 追踪tracking

    追踪 追踪部分的主要思路是在当前帧和(局部)地图之间寻找尽可能多的对应关系,来优化当前帧的位姿. 作者在追踪这部分主要用了几种模型:运动模型(Tracking with motion model).关 ...

  10. 吴裕雄--天生自然 pythonTensorFlow自然语言处理:Seq2Seq模型--训练

    import tensorflow as tf # 1.参数设置. # 假设输入数据已经用9.2.1小节中的方法转换成了单词编号的格式. SRC_TRAIN_DATA = "F:\\Tens ...