Leetcode 277 Solution

This article provides solution to leetcode question 277 (find-the-celebrity)

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;
    }
};