zoukankan      html  css  js  c++  java
  • [LeetCode] Find the Celebrity

    Problem Description:

    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.


    A classic and interesting problem. The idea is to maintain a list/set of candidates. Then at each interval we pick two of them and eliminate one. For example, if person a knows person b, we eliminate a; otherwise, we eliminate b. Finally, we will have a single candidate left. The only thing we need to do is to verify whether he/she is the celebrity using the definition (he/she does not know any other but all others know him/her).

    The following is a naive implementation of the above idea using vector. You may also use unordered_set.

     1 // Forward declaration of the knows API.
     2 bool knows(int a, int b);
     3 
     4 class Solution {
     5 public:
     6     int findCelebrity(int n) {
     7         vector<int> candidates(n);
     8         iota(candidates.begin(), candidates.end(), 0);
     9         while (candidates.size() >= 2) {
    10             int a = candidates.back(); candidates.pop_back();
    11             int b = candidates.back(); candidates.pop_back();
    12             if (knows(a, b)) candidates.push_back(b);
    13             else candidates.push_back(a);
    14         }
    15         return verifyCelebrity(candidates[0], n);
    16     }
    17 private:
    18     int verifyCelebrity(int c, int n) {
    19         for (int i = 0; i < n; i++) {
    20             if (i == c) continue;
    21             if (knows(c, i) || !knows(i, c))
    22                 return -1;
    23         }
    24         return c;
    25     }
    26 };

    This post shares a much simpler implementation, which is rewritten below.

     1 // Forward declaration of the knows API.
     2 bool knows(int a, int b);
     3 
     4 class Solution {
     5 public:
     6     int findCelebrity(int n) {
     7         int c = 0;
     8         for (int i = 1; i < n; i++)
     9             if (knows(c, i)) c = i;
    10         for (int i = 0; i < n; i++) {
    11             if (i == c) continue;
    12             if (!knows(i, c) || knows(c, i))
    13                 return -1;
    14         }
    15         return c;
    16     }
    17 };

    If you have intersts, you may read this nice note, which has a comprehensive discussion of the problem, written by Kevin Wayne from Princeton University. 

  • 相关阅读:
    解释一下什么是servlet?
    HTTP请求的GET与POST方式的区别
    金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出?
    Java中的异常处理机制的简单原理和应用。
    error和exception有什么区别?
    运行时异常与一般异常有何异同?
    jsp有哪些动作?作用分别是什么?
    jsp有哪些内置对象?作用分别是什么? 分别有什么方法?
    一个纸杯的测试用例
    白盒测试黑盒测试区别
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4787588.html
Copyright © 2011-2022 走看看