Find the Celebrity

要点:

  • 这题从solution反过来想比较好:loop through n同时maintain一个candidate:如果cand认识某个i,那么modify cand。实际上检查另一个条件i不认识cand也可以,因为这2个条件是exclusive的
  • 这个过程保证了cand不认识其之后的所有人。两个问题:
    • 一是会不会漏掉之前可能的celebrity呢?不会,如果当前cand不know i,i不可能是candidate。
    • 因为只是确保了之后的所有人,会不会是False positive呢?所以要再loop确认结果。所以两个loop一个验证必要条件,一个验证充分条件

https://repl.it/Cet1/1

错误点:

  • 最后验证的时候要两个条件都验证,否则0 knows 1, 1 knows 0的情况会错
# 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.

# Hide Company Tags LinkedIn Facebook
# Hide Tags Array # 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
"""
cand = 0
for i in xrange(1,n):
if knows(cand, i):
cand = i for i in xrange(n):
if i!=cand:
if not knows(i,cand) or knows(cand, i):
return -1
return cand

边工作边刷题:70天一遍leetcode: day 85的更多相关文章

  1. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  2. 边工作边刷题:70天一遍leetcode: day 77

    Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...

  3. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  4. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  5. 边工作边刷题:70天一遍leetcode: day 101

    dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...

  6. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...

  7. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

  8. 边工作边刷题:70天一遍leetcode: day 71-3

    Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...

  9. 边工作边刷题:70天一遍leetcode: day 71-2

    One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...

随机推荐

  1. android 6.0 httpclient

    Apache HTTP Client RemovalAndroid 6.0 release removes support for the Apache HTTP client. If your ap ...

  2. 如何安装nodejs

    1.进入官网https://nodejs.org/en/download/ 2.安装过程基本直接“NEXT”就可以了.(windows的安装msi文件在过程中会直接添加path的系统变量,变量值是你的 ...

  3. mysql命令行备份数据库

    MySQL数据库使用命令行备份|MySQL数据库备份命令 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:pass 数据库名称:myweb 备份数据库到D盘跟目录 mysq ...

  4. 移除NDK方法

    以下内容由:于伟建 提供 删除.project中的以下字段基本都是有cdt关键字的位置 删除.cproject然后重启eclipse,clean,重新编译我这里还有错误,就手动删了gen 删除包含cd ...

  5. [ javascript New Image() ] New Image() 对象讲解

    创建一个Image对象:var a=new Image();    定义Image对象的src: a.src="xxx.gif";    这样做就相当于给浏览器缓存了一张图片. 图 ...

  6. ListView的addHeaderView()方法相关问题

    使用listView.addHeaderView(view) 可以在 listView 上方添加一个view视图 ,使listView和这个view连接在一起 效果上看上去是一个整体 一般用于上拉刷新 ...

  7. UISlider显示进度(并且实现图片缩放)

    图片展示效果如下: 其他没什么好说的,直接上代码: RootView.h: #import <UIKit/UIKit.h> @interface RootView : UIView @pr ...

  8. iOS 简单工厂模式

    iOS 简单工厂模式 什么是简单工厂模式? 简单工厂模式中定义一个抽象类,抽象类中声明公共的特征及属性,抽象子类继承自抽象类,去实现具体的操作.工厂类根据外界需求,在工厂类中创建对应的抽象子类实例并传 ...

  9. IOS 杂笔-5(NSTimer极浅析)

    1.timer都会对它的target进行retain,我们需要小心对待这个target的生命周期问题,尤其是重复性的timer. 2. timer不是一种实时的机制,会存在延迟,而且延迟的程度跟当前线 ...

  10. C语言中的自定义函数

    C语言中可以使用系统函数也可以使用自己的函数,就是自定义函数 自定义函数分为四种 第一种:无参无返回值的 函数的声明 void sayH(); 函数的实现 void sayH(){ printf(&q ...