C++ O(n)


  • 0
    class Solution {
    public:
        int findCelebrity(int n) {
            int i = 0, j = 1;
            while(j < n){
                if(knows(i, j)) i = j;
                j++;
            }
            for(int k = 0; k < n; k++){
                if(k == i) continue;
                if(!knows(k, i) || knows(i, k)){
                    i = -1;
                    break;
                }
            }
            return i;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.