|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
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. |
8 | 14 | */
|
9 | 15 | public class _277 {
|
10 | 16 |
|
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 | + } |
16 | 24 | }
|
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 | + } |
21 | 29 | }
|
| 30 | + return candidate; |
22 | 31 | }
|
23 |
| - return candidate; |
24 |
| - } |
25 | 32 |
|
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 | + } |
29 | 37 | }
|
30 | 38 | }
|
0 commit comments