Skip to content

Commit c87ddd7

Browse files
add a solution for 277
1 parent 4b09485 commit c87ddd7

File tree

1 file changed

+34
-1
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+34
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,40 @@ public int findCelebrity(int n) {
1919
}
2020

2121
//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) {
2356
return false;
2457
}
2558
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy