LeetCode Find the Celebrity
原题链接在这里:https://leetcode.com/problems/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
.
题解:
先找一个candidate. 若是celebrity 认识 i, 说明i 有可能是celebrity. 就更新i为candidate.
找到这个candidate 后 再扫一遍来判定这是不是一个合格的candidate, 若是出现candidate认识i 或者 i不认识candidate的情况, 说明这不是一个合格的candidate.
Time Complexity: O(n). Space: O(1).
AC Java:
/* 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) {
if(n <= 1){
return -1;
}
int celebrity = 0;
//找一个candidate
for(int i = 0; i<n; i++){
if(knows(celebrity, i)){
celebrity = i;
}
}
for(int i = 0; i<n; i++){
//若是出现candidate认识i 或者 i不认识candidate的情况, 说明这不是一个合格的candidate
if(i != celebrity && (knows(celebrity, i) || !knows(i, celebrity))){
return -1;
}
}
return celebrity;
}
}
LeetCode Find the Celebrity的更多相关文章
- [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 (找到明星)$
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]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 ...
- [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 All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- 根据字体计算CGRect
UILabel *label = [[UILabel alloc]init]; label.numberOfLines = ;//多行显示 label.backgroundColor = [UICol ...
- hdu 2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路:将两个串连起来求一遍Next数组就行长度为两者之和,遍历时注意长度应该小于两个串中的最小值 ...
- Linux C编程学习之开发工具1---GCC编译器
GCC简介 GCC(GNU Compiler Collection)是一套功能强大.性能优越的编程语言编译器,它是GNU计划的代表作品之一.GCC以GPL和LGPL许可证发行,它是类Unix和苹果电脑 ...
- [BI项目记]-搭建代码管理环境之创建团队项目
此篇主要介绍如何基于TFS环境创建团队项目来进行项目代码的版本管理工作,这一系列将侧重于BI项目,当然对于其它项目也同样适用. 在TFS里开始一个项目,我们首先需要创建一个团队项目. 在Team Ex ...
- HDU3571 N-dimensional Sphere(高斯消元 同模方程)
每个点到中心距离相等,以第0个点为参考,其他n个点到中心距等于点0到中心距,故可列n个方程 列出等式后二次未知数相消,得到线性方程组 将每个数加上1e17,求答案是再减去,求解时对一个2 * (1e1 ...
- T-SQL 基础学习 03
局部变量 在T-SQL中,局部变量的名称必须以标记@作为前缀 语法 DECLARE @变量名数据类型 局部变量的赋值 方法一 SET @变量名 = 值 方法二 SELECT @变量名 = 值 SET和 ...
- AMR 转mp3 失败
private void changeToMp3(String sourcePath) { File source = new File(sourcePath); String mp3TargetPa ...
- [Note] Software Testing
Mocking: Difference between error and failure: TDD: BDD:
- ZeroMQ接口函数之 :zmq_tcp – 使用TCP协议的ØMQ网络单播协议
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq-tcp zmq_tcp(7) ØMQ Manual - ØMQ/4.1.0 Name zmq_t ...
- windows环境 andorid JNI开发
1.下载并安装GNUstep 下载地址:http://www.gnustep.org/experience/Windows.html 安装文件 a.GNUstep MSYS System b.GN ...