package com.fishercoder.solutions;

/**
 * 374. Guess Number Higher or Lower
 * We are playing the Guess Game. The game is as follows:
 * I pick a number from 1 to n. You have to guess which number I picked.
 * Every time you guess wrong, I'll tell you whether the number is higher or lower.
 * You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):

 -1 : My number is lower
 1 : My number is higher
 0 : Congrats! You got it!
 Example:
 n = 10, I pick 6.
 Return 6.

 */
public class _374 {
    /**The core problem/trouble to solve this problem is to figure out the problem description:
     * this API: guess(int num) means to take your guess num and let you know if your guessed num is bigger or smaller than the answer.
     * That's why if num > target, it returns -1 which means the target is smaller than your guess!!!*/

    public int guessNumber(int n) {
        int left = 1;
        int right = n;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            int g = guess(mid);
            if (g == 0) {
                return mid;
            } else if (g > 0) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (guess(left) == 0) {
            return left;
        }
        return right;
    }

    /**
     * This is a fake guess method that I wrote just to compile/test, I'll have to change it to another number other than 6 based on the number to be found.
     */
    private int guess(int num) {
        if (num > 6) {
            return -1;
        } else if (num < 6) {
            return 1;
        } else {
            return 0;
        }
    }

    public static void main(String... strings) {
        _374 test = new _374();
        System.out.println(test.guessNumber(1000));
    }

}

@@ -525,19 +525,71 @@
 4 %7B%0A    
-/**
+public static class Solution1 %7B%0A        /**%0A         * 
 The core
@@ -672,20 +672,24 @@
 ion:
+ this
 %0A     
-* this
+    *
  API
@@ -773,16 +773,27 @@
 d num is
+%0A         *
  bigger 
@@ -819,23 +819,16 @@
  answer.
-%0A     *
  That's 
@@ -877,16 +877,27 @@
 eans the
+%0A         *
  target 
@@ -929,12 +929,26 @@
 s!!!
-*/%0A%0A
+%0A         */%0A%0A    
     
@@ -975,24 +975,28 @@
 er(int n) %7B%0A
+    
         int 
@@ -1009,24 +1009,28 @@
  1;%0A        
+    
 int right = 
@@ -1032,16 +1032,20 @@
 ht = n;%0A
+    
         
@@ -1079,24 +1079,28 @@
             
+   
+ 
 int mid = le
@@ -1132,24 +1132,28 @@
             
+    
 int g = gues
@@ -1168,24 +1168,28 @@
             
+ 
+   
 if (g == 0) 
@@ -1202,24 +1202,28 @@
             
+  
+  
 return mid;%0A
@@ -1218,24 +1218,28 @@
 return mid;%0A
+    
             
@@ -1274,16 +1274,20 @@
         
+    
 left = m
@@ -1294,32 +1294,36 @@
 id;%0A            
+  
+  
 %7D else %7B%0A       
@@ -1307,32 +1307,36 @@
        %7D else %7B%0A
+    
                 
@@ -1352,32 +1352,36 @@
 id;%0A            
+    
 %7D%0A        %7D%0A    
@@ -1366,34 +1366,42 @@
       %7D%0A        
+ 
-%7D%0A
+   %7D%0A    
         if (gues
@@ -1408,32 +1408,36 @@
 s(left) == 0) %7B%0A
+    
             retu
@@ -1445,34 +1445,42 @@
 n left;%0A        
-%7D%0A
+    %7D%0A    
         return r
@@ -1493,19 +1493,27 @@
     
+   
+ 
 %7D%0A%0A
+    
     /**%0A
     
@@ -1504,24 +1504,28 @@
         /**%0A
+    
      * This 
@@ -1607,16 +1607,27 @@
 ge it to
+%0A         *
  another
@@ -1685,19 +1685,27 @@
 d.%0A     
+   
+ 
 */%0A
+    
     priv
@@ -1733,24 +1733,28 @@
 ) %7B%0A        
+    
 if (num %3E 6)
@@ -1764,24 +1764,28 @@
             
+  
+  
 return -1;%0A 
@@ -1783,16 +1783,20 @@
 urn -1;%0A
+    
         
@@ -1825,24 +1825,28 @@
             
+    
 return 1;%0A  
@@ -1843,32 +1843,36 @@
 turn 1;%0A        
+ 
+   
 %7D else %7B%0A       
@@ -1856,32 +1856,36 @@
        %7D else %7B%0A
+    
             retu
@@ -1902,149 +1902,23 @@
     
-%7D%0A
     %7D%0A
-%0A
     
-public static void main(String... strings) %7B%0A        _374 test = new _374();%0A        System.out.println(test.guessNumber(1000));
+    %7D
 %0A   

refactor 374