Leetcode 277 Solution
This article provides solution to leetcode question 277 (find-the-celebrity)
Access this page by simply typing in "lcs 277" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/find-the-celebrity
Solution
// Forward declaration of the knows API.
bool knows(int a, int b);
class Solution {
public:
int findCelebrity(int n) {
int res = 0;
for (int i = 0; i < n; i++)
if (knows(res, i))
res = i;
for (int i = 0; i < n; i++)
{
if (i == res)
continue;
if (knows(res, i) || knows(i, res) == false)
return -1;
}
return res;
}
};