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. ...
随机推荐
- DAOFactory复用代码
工厂设计模式 public class DaoFactory { private static final DaoFactory factory = new DaoFactory(); private ...
- Hibernate第四篇【集合映射、一对多和多对一】
前言 前面的我们使用的是一个表的操作,但我们实际的开发中不可能只使用一个表的-因此,本博文主要讲解关联映射 集合映射 需求分析:当用户购买商品,用户可能有多个地址. 数据库表 我们一般如下图一样设计数 ...
- Html在线编辑器--基于Jquery的xhEditor轻量级编辑器
xhEditor V1.2.2 下载地址 开源中国社区: http://www.oschina.net/p/xheditor xhEditor是一个基于jQuery开发的简单迷你并且高效的可视化XHT ...
- Apache Spark 2.2.0 中文文档 - 概述 | ApacheCN
Spark 概述 Apache Spark 是一个快速的, 多用途的集群计算系统. 它提供了 Java, Scala, Python 和 R 的高级 API,以及一个支持通用的执行图计算的优化过的引擎 ...
- python之time模块
from time import * ''' class struct_time(tuple): pass ''' tuple1 = (, , , , , , , , ) s = struct_tim ...
- Jenkins 在声明式 pipeline 中并行执行任务
在持续集成的过程中,并行的执行那些没有依赖关系的任务可以缩短整个执行过程.Jenkins 的 pipeline 功能支持我们用代码来配置持续集成的过程.本文将介绍在 Jenkins 中使用声明式 pi ...
- 深度学习框架caffe在macOS Heigh Sierra上安装过程实录
第一步.安装依赖库 brew install -vd snappy leveldb gflags glog szip lmdb brew tap homebrew/science brew insta ...
- [js高手之路] javascript面向对象写法与应用
一.什么是对象? 对象是n个属性和方法组成的集合,如js内置的document, Date, Regexp, Math等等 document就是有很多的属性和方法, 如:getElementById, ...
- 一脸懵逼学习基于CentOs的Hadoop集群安装与配置
1:Hadoop分布式计算平台是由Apache软件基金会开发的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS)和MapReduce(Google MapReduce的开源实现)为核心的 ...
- 给tableView设置headerView时遇到的问题
在ViewDidLoad里面设置了 self.tableView.tableHeaderView = 自定义的View 然后在模拟器上运行后,发现这个HeaderView挡住了后面的Cell,也就是c ...