Skip to content

Commit b862509

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 277_Find_the_Celebrity.java
1 parent 2594fc5 commit b862509

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Arrays/277_Find_the_Celebrity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution extends Relation {
2+
public int findCelebrity(int n) {
3+
int candidate = 0;
4+
5+
for (int i = 1; i < n; i++) {
6+
if (knows(candidate, i)) {
7+
candidate = i;
8+
}
9+
}
10+
11+
for (int i = 0; i < n; i++) {
12+
if (i == candidate) {
13+
continue;
14+
}
15+
16+
if (knows(candidate, i) || !knows(i, candidate)) {
17+
return -1;
18+
}
19+
}
20+
21+
return candidate;
22+
}
23+
}

0 commit comments

Comments
 (0)