clueless coding // TODO: be smarter

LeetCode Find the Celebrity: CPP Solution



TL;DR Code Solution


int findCelebrity(int n) {

     if(n<=1) return n;

int candidate = 0;

for(int i=1; i<n; i++){

    if ( !knows(i,candidate) ){
        candidate = i;
    }
}


for(int j=0; j<n; j++){

    if(j== candidate) continue;

    if( !knows(j,candidate) || knows(candidate,j) ){
          //if j does not know candidate, or candidate knows j, return -1;
        return -1;
    }

}


return candidate;

}