File tree 1 file changed +11
-0
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change 3
3
public class _277 {
4
4
5
5
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
+ */
6
15
public int findCelebrity (int n ) {
7
16
int candidate = 0 ;
8
17
for (int i = 1 ; i < n ; i ++) {
9
18
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.
10
21
candidate = i ;
11
22
}
12
23
}
You can’t perform that action at this time.
0 commit comments