// Source : https://leetcode.com/problems/first-bad-version/
// Inspired by : http://www.jiuzhang.com/solutions/first-bad-version/
// Author : Lei Cao
// Date : 2014-10-05
package firstBadVersion;
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class firstBadVersion extends VersionControl {
public int firstBadVersion(int n) {
if (n < 1) {
return 0;
}
int start = 1;
int end = n;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (super.isBadVersion(mid)) {
end = mid;
} else {
start = mid;
}
}
if (super.isBadVersion(start)) {
return start;
}
if (super.isBadVersion(end)) {
return end;
}
return 0;
}
}