package com.fishercoder.solutions;

/**
 * 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.
 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).
 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.
 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.
 */
public class _277 {

    public int findCelebrity(int n) {
        int candidate = 0;
        for (int i = 1; i < n; i++) {
            if (knows(candidate, i)) {
                candidate = i;
            }
        }
        for (int i = 0; i < n; i++) {
            if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) {
                return -1;
            }
        }
        return candidate;
    }

    //this is a mock-up method to make IDE happy.s
    private boolean knows(int i, int candidate) {
        return false;
    }
}

@@ -33,16 +33,46 @@
 s;%0A%0A/**%0A
+ * 277. Find the Celebrity%0A *%0A
  * Suppo
@@ -179,16 +179,19 @@
 lebrity.
+%0A *
  The def
@@ -299,16 +299,18 @@
 f them.%0A
+ *
  Now you
@@ -380,16 +380,19 @@
 not one.
+%0A *
  The onl
@@ -511,16 +511,19 @@
 knows B.
+%0A *
  You nee
@@ -645,16 +645,18 @@
 sense).%0A
+ *
  You are
@@ -835,16 +835,21 @@
  knows.%0A
+ *%0A *
  Note: T
@@ -1039,16 +1039,56 @@
 _277 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -1113,24 +1113,28 @@
 ty(int n) %7B%0A
+    
         int 
@@ -1148,32 +1148,36 @@
 te = 0;%0A        
+    
 for (int i = 1; 
@@ -1198,24 +1198,28 @@
             
+    
 if (knows(ca
@@ -1249,16 +1249,20 @@
         
+  
+  
 candidat
@@ -1272,34 +1272,42 @@
  i;%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -1300,32 +1300,36 @@
       %7D%0A        
+    
 for (int i = 0; 
@@ -1334,32 +1334,36 @@
 ; i %3C n; i++) %7B%0A
+    
             if (
@@ -1441,24 +1441,28 @@
             
+  
+  
 return -1;%0A 
@@ -1464,34 +1464,42 @@
 -1;%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -1496,24 +1496,28 @@
   %7D%0A        
+    
 return candi
@@ -1522,16 +1522,20 @@
 didate;%0A
+    
     %7D%0A%0A 
@@ -1537,16 +1537,20 @@
  %7D%0A%0A    
+  
+  
 //this i
@@ -1592,16 +1592,20 @@
 y.s%0A    
+   
+ 
 private 
@@ -1638,24 +1638,28 @@
 andidate) %7B%0A
+    
         retu
@@ -1668,16 +1668,26 @@
  false;%0A
+        %7D%0A
     %7D%0A%7D%0A

refactor 277