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. 

  • 相关阅读:
    网页截图软件
    JavaScript的写类方式(3)
    XMLHttpRequest与script对比
    JavaScript的写类方式(5)
    Java方法传值和传引用
    变量的显示/隐式声明
    用递归实现十进制数转换N进制
    Javascript中克隆一个数组
    JavaScript1.6数组新特性和JQuery的几个工具方法
    HTML P不能包含块级元素(包括自身)
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4787588.html
Copyright © 2011-2022 走看看