Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//TC: O(n): one pass to select a candidate (n−1 checks) + one pass to verify (O(n) checks).
//SC: O(1): constant extra variables.

//Pick candidate by scanning i=1..n−1; if candidate knows i, set candidate=i (others can’t be celeb).
//Verify candidate: for every i≠candidate, ensure candidate doesn’t know i and i knows candidate.
//If any verification fails return -1; otherwise return the candidate.

public class Solution extends Relation {
public int findCelebrity(int n) {
int ptnlCand = 0;

for(int i = 1; i < n; i++){
if(knows(ptnlCand, i)){
ptnlCand = i;
}
}

for(int i = 0; i < n; i++){
if(i == ptnlCand) continue;
if(i < ptnlCand){
if(knows(ptnlCand, i) || !knows(i, ptnlCand)) return -1;
} else {
if(!knows(i, ptnlCand)) return -1;
}
}

return ptnlCand;
}
}