[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.

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 now know any other while all other know him/her).

The following is a naive implementation of the above idea using vector. You may also use STL like 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 notes, which has a comprehensive discussion of the problem, written by Kevin Wayne from Princeton University. 

目录
相关文章
|
5月前
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
24 0
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
JavaScript 索引
LeetCode 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
57 0
LeetCode 436. Find Right Interval
LeetCode 389. Find the Difference
给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
84 0
LeetCode 389. Find the Difference
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
114 0
LeetCode 373. Find K Pairs with Smallest Sums
|
算法 Python
LeetCode 295. Find Median from Data Stream
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
62 0
LeetCode 295. Find Median from Data Stream
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
72 0
LeetCode 162. Find Peak Element
|
算法 索引 Python
<LeetCode天梯>Day017 字符串中的第一个唯一字符(哈希表+find&rfind) | 初级算法 | Python
<LeetCode天梯>Day017 字符串中的第一个唯一字符(哈希表+find&rfind) | 初级算法 | Python
<LeetCode天梯>Day017 字符串中的第一个唯一字符(哈希表+find&rfind) | 初级算法 | Python
LeetCode之Find the Difference
LeetCode之Find the Difference
96 0