Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Created April 21, 2020 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamkorg/5c7f9e05074213f7fa9b9388c5aafbe1 to your computer and use it in GitHub Desktop.
Save adamkorg/5c7f9e05074213f7fa9b9388c5aafbe1 to your computer and use it in GitHub Desktop.
Leetcode 277: Find the Celebrity (O(n))
#include <iostream>
using namespace std;
bool knows(int a, int b);
int findCelebrity(int n) {
if (n==0) return -1;
int celeb = 0;
for (int i=1; i<n; ++i) {
if (knows(celeb,i)) celeb=i;
}
for (int i=0; i<n; ++i) {
if (i!=celeb && (knows(celeb,i) || !knows(i,celeb))) return -1;
}
return celeb;
}
//not runnable, requires leetcode knows() function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment