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. ...
随机推荐
- webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】
实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...
- DAOFactory复用代码
工厂设计模式 public class DaoFactory { private static final DaoFactory factory = new DaoFactory(); private ...
- springmvc03-异常处理-静态文件
1,一个简单的登录 login.jsp页面 <%@ page language="java" contentType="text/html; charset=UTF ...
- 1 Spring Cloud Eureka服务治理(下)
注:此随笔为读书笔记.<Spring Cloud微服务实战> 上篇主要介绍了什么是微服务以及微服务治理的简单实现,如微服务注册中心的实现.微服务注册的实现.微服务的发现和消费的实现.微服务 ...
- [AHOI2001]质数和分解
[AHOI2001]质数和分解 题目描述 任何大于 1 的自然数 n 都可以写成若干个大于等于 2 且小于等于 n 的质数之和表达式(包括只有一个数构成的和表达式的情况),并且可能有不止一种质数和的形 ...
- IDEA用maven创建springMVC项目和配置
工具准备:IDEA2016.3 Java jdk 1.8 1.DEA创建项目 新建一个maven project,并且选择webapp原型. 然后点击next 这里的GroupId和Artifac ...
- CentOS更新源
1.首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...
- 我的第一个python web开发框架(2)——一个简单的小外包
第一部分说明 第一部分大概有20来章,主要讲的是一些开发常识.开发前中后期准备内容.开发环境与服务器部署环境安装设置.python基础框架结构与功能等内容,代码会比较简单. 本系列会以故事的方式,向大 ...
- 用ES6巧妙的解决传统面试中的算法小问题!
最近自己也在准备面试,在复习算法的时候,机智的用了一波ES6.一起来瞧瞧吧! 1.数组的去重 var arr=str.split(''); for(var i=0;i<arr.length-1; ...
- hdu4686 Arc of Dream 2013 Multi-University Training Contest 9矩阵快速幂
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Tot ...