File tree Expand file tree Collapse file tree 1 file changed +34
-1
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,40 @@ public int findCelebrity(int n) {
19
19
}
20
20
21
21
//this is a mock-up method to make IDE happy.s
22
- private boolean knows (int i , int candidate ) {
22
+ boolean knows (int i , int candidate ) {
23
+ return false ;
24
+ }
25
+ }
26
+
27
+ public static class Solution2 {
28
+ /**
29
+ * My completely original solution on 10/21/2021, which turns out to match https://leetcode.com/problems/find-the-celebrity/solution/ Solution 1.
30
+ * Time: O(n^2)
31
+ * Space: O(1)
32
+ */
33
+ public int findCelebrity (int n ) {
34
+ for (int i = 0 ; i < n ; i ++) {
35
+ //check if i is the celebrity
36
+ int j = 0 ;
37
+ for (; j < n ; j ++) {
38
+ if (i != j ) {
39
+ if (knows (i , j )) {
40
+ break ;
41
+ }
42
+ if (!knows (j , i )) {
43
+ break ;
44
+ }
45
+ }
46
+ }
47
+ if (j == n ) {
48
+ return i ;
49
+ }
50
+ }
51
+ return -1 ;
52
+ }
53
+
54
+ //this is a mock-up method to make IDE happy.s
55
+ boolean knows (int i , int candidate ) {
23
56
return false ;
24
57
}
25
58
}
You can’t perform that action at this time.
0 commit comments