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. MyEclipse+Mysql (一)

     MyEclipse连接Mysql数据库   准备工作:MyEclipse使用的是2013版,mysql  Ver 14.14 Distrib 5.6.28   1.jar包的下载(jdbc驱动) 我 ...

  2. R语言-神经网络包RSNNS

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  3. QT分页控件,开源,供大家使用

    下载地址:http://files.cnblogs.com/dragonsuc/qt5.rar

  4. [Windows Phone] APP上架,遇到错误2001的解决方案。(Error:2001)

    [Windows Phone] APP上架,遇到错误2001的解决方案.(Error:2001) 问题情景 最近在开始玩Windows Phone的开发,开发的过程中虽然有点小挫折,但是参考网络许多前 ...

  5. AngularJS的指令用法

    scope的绑定策略: @ :把当前属性作为字符串传递,你还可以绑定来自外层scope的值,在属性值中插入 {{}}即可 示例代码: scopeAt.html <!doctype html> ...

  6. SharePoint 使用代码为页面添加WebPart

    传统的SharePoint实施中,我们通常会创建SharePoint页面,然后添加webpartzone,而后在上面添加webpart:但是有些情况下,也要求我们使用代码,将webpart添加到相应w ...

  7. [ html canvas putImageData ] canvas绘图属性 putImageData 属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  8. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q85-Q87)

    Question 85You create a Microsoft .NET Framework console application that uses a Representational St ...

  9. java多线程系列1--线程实现与调度

    java的重要功能之一就是内部支持多线程,这一系列文章将详细剖析java多线程的基础知识 多线程概述 多线程引入 程序只有一个执行流程,所以这样的程序就是单线程程序. 假如一个程序有多条执行流程,那么 ...

  10. Android Small插件化框架解读——Activity注册和生命周期

    通过对嵌入式企鹅圈原创团队成员degao之前发表的<Android Small插件化框架源码分析>的学习,对Android使用的插件化技术有了初步的了解,但还是有很多需要认真学习的地方,特 ...