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 ...
随机推荐
- iOS - 使用自定义字体-苹方字体
苹方提供了六个字重,font-family 定义如下:苹方-简 常规体font-family: PingFangSC-Regular, sans-serif;苹方-简 极细体font-family: ...
- C++ 基础知识复习(六)
操作系统部分: 79. 操作系统的最小调度单位:线程. 线程thread,进程process.一个进程至少包含一个线程,主线程,main thread. 80. 资源的最小单位是:进程. 81. 进程 ...
- 几年前做家教写的C教程(之四专讲了指针与汉诺塔问题)
C语言学习宝典(4) 指针:可以有效的表示复杂的数据结构,能动态的分配动态空间,方便的使用字符串,有效的使用数组,能直接处理内存单元 不掌握指针就没有掌握C语言的精华 地址:系统为每一个变量分配一个内 ...
- java基础(四)
一.面向对象的三个基本特征: 1.封装,将对象的实现细节隐藏起来,并通过公共接口暴露相关功能: 2.继承,代码复用的表现,当子类继承父类后,子类作为一种特殊的父类,直接获得父类的属性和方法: 3.多态 ...
- 【转】C#中WinForm程序退出方法技巧总结
C#中WinForm程序退出方法技巧总结 一.关闭窗体 在c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.Ex ...
- zookeeper心跳机制流程梳理
zookeeper心跳机制流程梳理 Processor链Chain protected void setupRequestProcessors() { RequestProcessor finalPr ...
- bzoj4364: [IOI2014]wall砖墙
线段树打标记的好(luo)题 打打标记,记得下移 = =听说2000000是用来卡线段树的 = =怎么办呢,,, = =打个读入优化看看能不能卡过去吧 #include<cstdio> # ...
- java的poi技术读,写Excel[2003-2007,2010]
在上一篇blog:java的poi技术读取Excel[2003-2007,2010] 中介绍了关于java中的poi技术读取excel的相关操作 读取excel和MySQL相关: java的poi技术 ...
- VS2013 - 自定义新建文件模版
一直想统一下项目中的关于-(新建文件时,添加个人信息,如:创建者,创建时间等个性化信息). 从网络上学习到方法很简单,只需要把IDE安装目录下的模板进行修改保存,即可每次创建拥有固定的模板呈现. 具体 ...
- UI--UIPickerView和UIDatePicker的总结
回到顶部 UIPickerView的主要方法和城市选择器的修正 UIPickerView只有两个数据源方法.要想完整地显示出PickerView,需要结合使用代理方法 数据源方法: // 一共有多少组 ...