Created
April 21, 2020 22:12
-
-
Save adamkorg/5c7f9e05074213f7fa9b9388c5aafbe1 to your computer and use it in GitHub Desktop.
Leetcode 277: Find the Celebrity (O(n))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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