package com.fishercoder.solutions;

/**You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

 Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

 You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.*/
public class _278 {

	public int firstBadVersion(int n) {
		int left = 1;
		int right = n;
		if (isBadVersion(left)) {
			return left;
		}

		while (left + 1 < right) {
			int mid = left + (right - left) / 2;
			if (isBadVersion(mid)) {
				right = mid;
			} else {
				left = mid;
			}
		}

		if (isBadVersion(left)) {
			return left;
		}
		return right;
	}

	private boolean isBadVersion(int left) {
		//this is a fake method to make Eclipse happy
		return false;
	}

}

@@ -32,16 +32,49 @@
 ns;%0A%0A/**
+%0A * 278. First Bad Version%0A *%0A * 
 You are 
@@ -145,16 +145,19 @@
 product.
+%0A *
  Unfortu
@@ -223,16 +223,19 @@
 y check.
+%0A *
  Since e
@@ -341,17 +341,18 @@
 so bad.%0A
-%0A
+ *
  Suppose
@@ -430,16 +430,19 @@
 bad one,
+%0A *
  which c
@@ -481,17 +481,21 @@
 be bad.%0A
-%0A
+ *%0A *
  You are
@@ -576,16 +576,19 @@
  is bad.
+%0A *
  Impleme
@@ -707,16 +707,50 @@
 _278 %7B%0A%0A
+%09public static class Solution1 %7B%0A%09
 %09public 
@@ -778,16 +778,17 @@
 nt n) %7B%0A
+%09
 %09%09int le
@@ -797,16 +797,17 @@
  = 1;%0A%09%09
+%09
 int righ
@@ -813,16 +813,17 @@
 ht = n;%0A
+%09
 %09%09if (is
@@ -834,32 +834,33 @@
 ersion(left)) %7B%0A
+%09
 %09%09%09return left;%0A
@@ -861,19 +861,21 @@
 left;%0A%09%09
+%09
 %7D%0A%0A
+%09
 %09%09while 
@@ -891,24 +891,25 @@
  %3C right) %7B%0A
+%09
 %09%09%09int mid =
@@ -936,16 +936,17 @@
 t) / 2;%0A
+%09
 %09%09%09if (i
@@ -969,16 +969,17 @@
 ) %7B%0A%09%09%09%09
+%09
 right = 
@@ -986,16 +986,17 @@
 mid;%0A%09%09%09
+%09
 %7D else %7B
@@ -996,16 +996,17 @@
  else %7B%0A
+%09
 %09%09%09%09left
@@ -1020,15 +1020,18 @@
 %0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A%0A
+%09
 %09%09if
@@ -1054,16 +1054,17 @@
 eft)) %7B%0A
+%09
 %09%09%09retur
@@ -1073,18 +1073,20 @@
 left;%0A%09%09
+%09
 %7D%0A
+%09
 %09%09return
@@ -1093,20 +1093,22 @@
  right;%0A
+%09
 %09%7D%0A%0A
+%09
 %09private
@@ -1141,16 +1141,17 @@
 left) %7B%0A
+%09
 %09%09//this
@@ -1190,16 +1190,17 @@
 e happy%0A
+%09
 %09%09return
@@ -1207,14 +1207,17 @@
  false;%0A
+%09
 %09%7D%0A
+%09%7D
 %0A%7D%0A

refactor 278