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 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.
题目标签:Array
题目给了我们一个n, 代表 有n个人,从0 到n-1。其中可能会有一个明星,或者没有明星,让我们通过,问每个人问题的方式,找到谁是明星或者没人是明星。问题的方式是:问A,你认识B吗? 可以得到是 或者 不是的回答。明星的定义是:明星不认识任何人,但是所有人都要认识明星。
我们可以利用遍历n两次来找出谁是明星:
第一次遍历:假设第一个人是明星;所有人站一排0 到 n-1。
依次问每一个人X,你认识下一个人吗?
如果认识: 假设下一个人是明星;
如果不认识:假设不变,X依然是。
如果有明星再这一排人里的话,当问到明星前面一个人的时候,肯定会把明星设为假设,而之后问明星 认不认识 之后的所有人的时候,得到的肯定是 否定的回答,所以假设会留在明星这里。
第二次遍历:因为不一定有明星,所以还要遍历一次来确定假设的是不是明星。
依次问每一个人X(除了假设的明星之外), X认识假设的明星吗 还要问 假设的明星 认识X吗?
如果不是真的明星,他/她 不会被所有人认识 或者 他/她 会认识一些人。
Java Solution:
Runtime beats 80.26%
完成日期:09/09/2017
关键词:Array
关键点:需要2次遍历:1次 用来挑出 候选; 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 candidate = 0; // first iteration: find out the candidate
for(int i=1; i<n; i++)
{
if(knows(candidate, i))
candidate = i;
} // second iteration: make sure candidate is a celebrity
for(int i=0; i<n; i++)
{
if(i == candidate) // no need to ask candidate
continue; // everyone else should know celebrity and celebrity should not know anyone else
if(!knows(i, candidate) || knows(candidate, i))
return -1;
} return candidate;
}
}
参考资料:
https://leetcode.com/problems/find-the-celebrity/discuss/
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 277. Find the Celebrity (找到明星)$的更多相关文章
- 名人问题 算法解析与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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 【LeetCode】277. Find the Celebrity 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://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 exi ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- [LeetCode] Find Eventual Safe States 找到最终的安全状态
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
随机推荐
- Day-14: 常用的内建模块
collections包含对tuple.list.dict等派生出新功能 namedtuple用来为tuple类型派生出一个新名字的tuple类,并提供用属性引出的功能. >>> f ...
- Oracle总结第三篇【PLSQL】
PLSQL介绍 PLSQL是Oracle对SQL99的一种扩展,基本每一种数据库都会对SQL进行扩展,Oracle对SQL的扩展就叫做PLSQL- SQL99是什么 (1)是操作所有关系型数据库的规则 ...
- JS(三)
上周介绍了JS中两个比较重要的东西,循环和函数,这周再给大家介绍一下BOM和DOM 一.BOM 1.首先来说一下什么是BOM,BOM即浏览器对象模型,说白一点就是与浏览器进行的交互的对象模型. 2.B ...
- PHPFastCGI进程管理器PHP-FPM详解
PHP-FPM是一个PHPFastCGI进程管理器,是只用于PHP的. PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将它patch到你的PH ...
- 使用C语言和Java分别实现冒泡排序和选择排序
经典排序算法--冒泡和选择排序法 Java实现冒泡排序 基本思想是,对相邻的元素进行两两比较,顺序相反则进行交换,这样,每一趟会将最小或最大的元素放到顶端,最终达到完全有序,首先看个动图: 我们要清楚 ...
- java之装饰器模式
Decorator Pattern(装饰器模式),定义:Attach additional responsibilities to an object dynamically. Decorators ...
- Ubuntu16笔记本双显卡安装NVIDIA驱动
blockquote { direction: ltr; color: rgb(0, 0, 0) } blockquote.western { font-family: "Liberatio ...
- Python网络编程socket练习(TCP)
服务器端:server.py # -*- coding: utf-8 -*- from socket import * HOST='' PORT=5000 BUFF_SIZE=1024 ADDR=(H ...
- nodejs+express+mongoose无法获取数据库数据问题解决
通过mongoose与mongodb进行操作.而mongoose是通过model来创建mongodb中对应的collection的,这样你通过如下的代码: mongoose.model('User', ...
- php中常用的字符串截取函数mb_substr实例解释
string mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_ ...