名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)
1. 题目描述 Problem Description
Leetcode 277. Find the Celebrity
Suppose you are at a party with
n
people (labeled from0
ton - 1
) and among them, there may exist one celebrity. The definition of a celebrity is that all the othern - 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 functionint findCelebrity(n)
, your function should minimize the number of calls toknows
.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
.
2. 题目解析(参考了Prof. Sarrafzadeh 180讲的内容)
首先题目定义了:如果一个人A是名人,那么其他的n-1个人都认识A,而且A不认识其他的n-1个人。这题的目标是要从n个人中找出其中的名人,如果没有,则告知其中没有名人。我们可以调用knows(a,b)函数来询问a是否认识b,而我们要尽量少调用这个函数。
这题的关键是理解题意,题意其实比较容易理解错,所以有几个小问题可以帮大家理解:
(1)n个人中最多可以有几个名人?
答案:是1个。如果这n个人中有2个名人分别为A和B,那么按照定义,如果一个人A是名人,那么其他的n-1个人都认识A,而且A不认识其他的n-1个人,这也就是说A不认识B。但与此同时我们又定义了如果一个人B是名人,那么其他的n-1个人都认识B,那么A也应该认识B。这就产生了 contradiction了。因此其中不可以有2个或2个以上的名人。
(2) 如果其他n-1个人都认识A,但是A认识了n-1个人中其中一个人,那么A还是名人吗?
答案:不是的。
(3) 如果A不认识其他的n-1个人,但是n-1个人中有人不认识A,那么A还是名人吗?
答案:不是的。
-
这题最直接的想法应该是暴力,但是暴力的复杂度是多少呢?对于每个人我们需要询问:
(1) 他是否认识剩下的n-1个人: 最坏的情况需要调用knows(a,b)函数n-1次
(2) 剩下的n-1个人是否认识他:最坏的情况需要调用knows(a,b)函数n-1次
有的可能会重复,但是总体来说需要询问的次数是$\frac{n(n-1)}{2}$。即时间复杂度为 O($n^2$)
-
有没有办法优化算法?
如果我们从n个人中任意挑两个人a,b出来,询问啊是否认识b,那么只有两种情况:
(1)a认识b:那么a肯定不是名人。
(2)b认识a:那么b肯定不是名人。
所以任何一种情况,我们都可以排除掉2个人中的1一个人。如果我们不断地重复的这个过程,直到只剩下一个人,那么我们会做n-1次对比。而剩下这个人是唯一可能成为名人的人,那么我们需要询问剩下的n-1个人是否认识他,也需要询问他是否认识剩下的n-1个人。
因此我们一共需要询问3(n-1)次——时间复杂度为O($n$)。
3. Python 实现
# The knows API is already defined for you. # @param a, person a # @param b, person b # @return a boolean, whether a knows b # def knows(a, b): class Solution(object): def findCelebrity(self, n): """ :type n: int :rtype: int """ if n == 0: return -1 curr_stay = 0 for i in range(1, n): if knows(curr_stay, i): curr_stay = i for i in range(0, n): if i == curr_stay: continue if knows(curr_stay, i): return -1 if not knows(i, curr_stay): return -1 return curr_stay
名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)的更多相关文章
- DeepFM算法解析及Python实现
1. DeepFM算法的提出 由于DeepFM算法有效的结合了因子分解机与神经网络在特征学习中的优点:同时提取到低阶组合特征与高阶组合特征,所以越来越被广泛使用. 在DeepFM中,FM算法负责对一阶 ...
- GBDT+LR算法解析及Python实现
1. GBDT + LR 是什么 本质上GBDT+LR是一种具有stacking思想的二分类器模型,所以可以用来解决二分类问题.这个方法出自于Facebook 2014年的论文 Practical L ...
- FM算法解析及Python实现
1. 什么是FM? FM即Factor Machine,因子分解机. 2. 为什么需要FM? 1.特征组合是许多机器学习建模过程中遇到的问题,如果对特征直接建模,很有可能会忽略掉特征与特征之间的关联信 ...
- Bayesian Personalized Ranking 算法解析及Python实现
1. Learning to Rank 1.1 什么是排序算法 为什么google搜索 ”idiot“ 后,会出现特朗普的照片? “我们已经爬取和存储了数十亿的网页拷贝在我们相应的索引位置.因此,你输 ...
- FFM算法解析及Python实现
1. 什么是FFM? 通过引入field的概念,FFM把相同性质的特征归于同一个field,相当于把FM中已经细分的feature再次进行拆分从而进行特征组合的二分类模型. 2. 为什么需要FFM? ...
- [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谁是名人
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- python常见排序算法解析
python——常见排序算法解析 算法是程序员的灵魂. 下面的博文是我整理的感觉还不错的算法实现 原理的理解是最重要的,我会常回来看看,并坚持每天刷leetcode 本篇主要实现九(八)大排序算法 ...
随机推荐
- C#自定义异常
继承自System.ApplicationException类,并使用Exception作为自定义异常类名的结尾 三个构造函数:一个无参构造函数:一个字符串参数的构造函数:一个字符串参数,一个内部异常 ...
- Redis支持的五种数据类型
redis支持的五种数据类型: 1.string(字符串) 2.hash(哈希) Redis hash 是一个键值(key=>value)对集合. Redis hash是一个string类型的f ...
- 【laravel】passport的scope作用域
1.根据作用域生成token $user->createToken($request->name,['test1'])->accessToken; 2.注册中间件 'scopes' ...
- kafka zk常用命令
1 创建topic: kafka-topics.sh --create --zookeeper 3.3.3.3:2181 --replication-factor 1 --partitions 3 ...
- 863. All Nodes Distance K in Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- vuejs中的生命周期
vue中生命周期分为初始化,跟新状态,销毁三个阶段 1.初始化阶段:beforeCreated,created,beforeMount,mounted 2.跟新状态:beforeUpdate,upda ...
- [原创]利用python构造ICMP Echo Request并发送
import socket import struct def checksum(source_string): sum = 0 countTo = (len(source_string)/2)*2 ...
- 单例模式之pymysql运用实例
何为单例? 简单介绍一下下:单例是个什么鬼东西!!!! 单例模式含义] 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而 ...
- CLR via c#读书笔记五:方法
注:书本第8章:方法 实例构造器和类(引用类型) 构造器方法在“方法定义元数据表”中始终叫做.ctor(constructor的简称). 构造引用类型的对象,在调用类型的实例构造器之前,为对象分配的内 ...
- 能用程序解决的问题绝不BB之租房篇章...
项目缘起于高德API+Python解决租房问题, 修修补补之后上线了58公寓高德搜房(全国版)http://woyaozufang.live:8080. 经过了多次代码优化.内容改版.新增房源等... ...