Skip to content

Commit 365ec1c

Browse files
add a solution for 277
1 parent c87ddd7 commit 365ec1c

File tree

1 file changed

+11
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+11
-0
lines changed

src/main/java/com/fishercoder/solutions/_277.java

+11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
public class _277 {
44

55
public static class Solution1 {
6+
/**
7+
* Credit: https://leetcode.com/problems/find-the-celebrity/solution/ approach 2
8+
* 1. we narrow down the possible celebrity candidate to only one person
9+
* 2. we check to make sure that every other person knows
10+
* this candidate and this candidate doesn't know any one of them, otherwise return -1
11+
*
12+
* We can think of this is a directed graph problem, a total of n vertices, the one vertex that has zero outgoing edges
13+
* and n - 1 incoming edges is the celebrity.
14+
*/
615
public int findCelebrity(int n) {
716
int candidate = 0;
817
for (int i = 1; i < n; i++) {
918
if (knows(candidate, i)) {
19+
//this rules out the possibility that candidiate is a celebrity since he/she knows i
20+
//so we update candidate to be i, because at least i doesn't know anybody yet.
1021
candidate = i;
1122
}
1223
}

0 commit comments

Comments
 (0)