Skip to content

Commit 7ee0e55

Browse files
refactor 277
1 parent f31ac9b commit 7ee0e55

File tree

1 file changed

+26
-18
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+26
-18
lines changed
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.
5-
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
6-
You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows.
7-
Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.
4+
* 277. Find the Celebrity
5+
*
6+
* Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity.
7+
* The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.
8+
* Now you want to find out who the celebrity is or verify that there is not one.
9+
* The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B.
10+
* You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
11+
* You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows.
12+
*
13+
* Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.
814
*/
915
public class _277 {
1016

11-
public int findCelebrity(int n) {
12-
int candidate = 0;
13-
for (int i = 1; i < n; i++) {
14-
if (knows(candidate, i)) {
15-
candidate = i;
17+
public static class Solution1 {
18+
public int findCelebrity(int n) {
19+
int candidate = 0;
20+
for (int i = 1; i < n; i++) {
21+
if (knows(candidate, i)) {
22+
candidate = i;
23+
}
1624
}
17-
}
18-
for (int i = 0; i < n; i++) {
19-
if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) {
20-
return -1;
25+
for (int i = 0; i < n; i++) {
26+
if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) {
27+
return -1;
28+
}
2129
}
30+
return candidate;
2231
}
23-
return candidate;
24-
}
2532

26-
//this is a mock-up method to make IDE happy.s
27-
private boolean knows(int i, int candidate) {
28-
return false;
33+
//this is a mock-up method to make IDE happy.s
34+
private boolean knows(int i, int candidate) {
35+
return false;
36+
}
2937
}
3038
}

0 commit comments

Comments
 (0)