The code base version is a integer and start from 1 to N. One day, someone commits a bad version of the code case, so it caused itself and the following versions is all failed in t He unit tests.
You can determine whether a version was bad by the following interface:
java:
public VersionControl {
    }
c++:
class VersionControl {
public:
bool isbadversion (int version);
};
python:
class VersionControl:
Find the first bad version.
Note
You should call Isbadversion as few as possible.
Please read the annotation in code area to get the correct-on-the-isbadversion in different language. For example, Java is versioncontrol.isbadversion.
Example
Given n=5
Call Isbadversion (3), Get false
Call Isbadversion (5), Get True
Call Isbadversion (4), Get True
Return 4 is the first bad version
Challenge
Don't call isbadversion exceed O (Logn) times.
Solution:
1 /**2 * public class VersionControl {3 * public static Boolean isbadversion (int k);4 * }5 * can use Versioncontrol.isbadversion (k) to judge wether6 * The KTH code version is bad or not.7 */8 classSolution {9 /**Ten * @paramn:an integers. One * @return: An integer which are the first bad version. A */ - Public intFindfirstbadversion (intN) { - if(n==1)return1; the - intStart = 1; - intEnd =N; - while(start<=end) { + intMID = start + (End-start)/2; - + if(!Versioncontrol.isbadversion (mid)) AStart = Mid +1; at Else { - if(Mid==1 | |! Versioncontrol.isbadversion (mid-1)) - returnmid; - ElseEnd = Mid-1; - } - } in - return-1; to } +}
Lintcode-first Bad Version