[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
.
题意:
有一坨人,只允许你问“谁认识谁”。得想个法儿把名人给找出来。什么叫名人?谁都认识TA,TA谁也不认。
想起来牛姐有木有?
Solution:
只要call一次 knows(i, j) by comparing a pair (i, j), we are able to discard one of them
1.Find a candidate by one pass: make sure other people are not celebrities.if knows(i, j) is true, i is guaranteed not to be celebrityotherwise, j is not a celebrity
2.Make sure if the candidate is the celebrity by one pass
code
/* 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;
// assume candidate is celebrity
// if candidate knows i, such candidate isn't celebrity, try another one
for(int i = 1; i < n; i++){
if(knows(candidate, i))
candidate = i;
} for(int i = 0; i < n; i++){
// we don't want to check if person knows himself
// if candidate knows i, such candidate isn't celebrity
// if nobody knows candidate, such candidate isn't celebrity
if(candidate!=i && (knows(candidate, i) || !knows(i, candidate))) return -1;
}
return candidate;
}
}
[leetcode]277. Find the Celebrity 找名人的更多相关文章
- [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 ...
- 名人问题 算法解析与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] 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] Lemonade Change 买柠檬找零
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
随机推荐
- 关于input=file的用法
<input type="file"/>这个东西是用来上传图片用的. 1,但是存在一下问题但是在在各个浏览器下的显示是不一样的 IE下: IE之外的浏览器: 2.如果不 ...
- 【Codeforces】CF 9 D How many trees?(dp)
题目 传送门:QWQ 分析 用$ dp[i][j] $表示用i个节点,有多少深度小于等于j的二叉树. 答案是$ dp[n][n] - dp[n][h-1] $ 转移时枚举左子树的节点数量,就可以知道右 ...
- centos 7.x设置守护进程的文件数量限制
在Bash中有个ulimit命令,提供了对Shell及该Shell启动的进程的可用资源控制.主要包括打开文件描述符数量.用户的最大进程数量.coredump文件的大小等. 1. 系统级设置 1.1 C ...
- PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...
- php trim() 函数实例讲解
php trim() 函数移除字符串两侧的空白字符或其他预定义字符,本文章向码农介绍php trim() 函数的使用方法和实例,感兴趣的码农可以参考一下. 定义和用法 trim() 函数移除字符串两侧 ...
- Java并发编程学习路线(转)
以前特地学过并发编程,但是没怎么学进去,不太喜欢.最近发现,作为一个资深工程师,却没有完整深入系统的学习过,而反是现在的BAT大并发是必须的,感觉甚是惭愧. 故找了一片学习文章,如下,准备集中一段时间 ...
- case功能菜单选项
脚本aim; 实现如下功能菜单并实现相应的功能;同在之前公司的一个游戏控制脚本; 1),显示当前时间; 2),显示cpu负载 3),显示剩余内存 0),退出脚本 如图菜单界面
- 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结
第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...
- OpenACC 优化矩阵乘法
▶ 按书上的步骤使用不同的导语优化矩阵乘法 ● 所有的代码 #include <iostream> #include <cstdlib> #include <chrono ...
- controller检查header
以前都只能拿到request再检查,其实有相应的注解. public Result updateRecentScore(@RequestBody Map map, @RequestHeader(&qu ...