[LC] 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). 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.
/* 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 res = 0;
// find the celebrity
for (int i = 0; i < n; i++) {
if (knows(res, i)) {
res = i;
}
} // return -1 if res is not celebrity
for (int i = 0; i < n; i++) {
if (i != res && (knows(res, i) || !knows(i, res))) {
return -1;
}
}
return res;
}
}
[LC] 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 ...
- 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#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 ...
- [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 ...
- <Array> 277 243 244 245
277. Find the Celebrity knows(i, j): By comparing a pair(i, j), we are able to discard one of them 1 ...
随机推荐
- 【leetcode困难】968. 监控二叉树
968. 监控二叉树 瞎**分析评论区Rui大佬的答案,这题想直接递归return min还是有坑的,分计数和状态.有个状态转换的思想
- Python—程序设计:单例模式
单例模式 单例模式(Singleton Pattern)属于创建型模式,它提供了一种创建对象的最佳方式.这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建,并提供一种访问其 ...
- XCOM串口助手打印不出数据
本次实验是在基于原子的战舰开发板上的做定时器捕获实验,程序源码下载到板子上运行正常.指示灯正常显示,打开XCOM识别不来串口,原因:硬件上没有插USB转串口线: 连接上USB转串口线,软件上以显示CH ...
- APP-计算器
在网课上学习了计算器的制作方法,并自己做了小常识,看完一便后,感觉自己会了,思想也明白了,但是当关掉视频真正开始做的时候会发向许多的问题,自己在前的好多语法用的并不是很熟练,但是反复的查阅资料,观看教 ...
- 18 11 11 网络通信大都数使用的方式 socket
---恢复内容开始--- 浏览器 和 聊天工具 一般都用socket socket 在不同的 语言中的使用流程都大同小异 收 发 关闭 import socket def len() ...
- -mtime
大家在使用find命令中的mtime参数时候,会看到官方的解释如下: -mtime n File's data was last modified n*24 hours ...
- uploadifive使用笔记
官网地址:http://www.uploadify.com/ uploadifive 是基于H5开发,所以支持移动端和PC端 <input type="file" name= ...
- 201503-2 数字排序 Java
思路: 将出现过的数以及次数放进Map中,转成List,用Comparator就可以排序了.参数中o2-o1,与排序规则相反,为降序 import java.util.ArrayList; impor ...
- 02)MFC那几个基本文件介绍
1)首先是 类目录: 2)在这个工程里面,你找不到主函数,没有主函数,你能看到的 仅仅有这五个类 但是 你还看不到 这五个类对应的对象子啊哪里 而且 我们在写MFC程序的时候 我压 ...
- python——print函数
.print()函数概述 print() 方法用于打印输出,是python中最常见的一个函数. 该函数的语法如下: print(*objects, sep=' ', end='\n', file=sy ...