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 returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Input:
n = 5, bad = 4
Output:
4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
Since all versions after the first bad version are also bad, we can use binary search to efficiently find the first bad version. At each step, we check the middle version:
If it is bad, the first bad version must be at or before this version.
If it is not bad, the first bad version must be after this version.
We recursively narrow the search range until we find the first bad version.
// The API isBadVersion(version) is defined for you.
classSolution {
public:int firstBadVersion(int n) {
returnhelper(1, n);
}
inthelper(int left, int right) {
if (left >= right) return left;
int mid = left + (right - left) /2;
if (isBadVersion(mid)) return helper(left, mid);
elsereturn helper(mid +1, right);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// The isBadVersion API is defined for you.funcfirstBadVersion(nint) int {
varhelperfunc(left, rightint) inthelper = func(left, rightint) int {
ifleft>=right {
returnleft }
mid:=left+ (right-left)/2ifisBadVersion(mid) {
returnhelper(left, mid)
}
returnhelper(mid+1, right)
}
returnhelper(1, n)
}
1
2
3
4
5
6
7
8
9
10
11
12
// The isBadVersion API is defined for you.publicclassSolutionextends VersionControl {
publicintfirstBadVersion(int n) {
return helper(1, n);
}
privateinthelper(int left, int right) {
if (left >= right) return left;
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) return helper(left, mid);
elsereturn helper(mid + 1, right);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// The isBadVersion API is defined for you.
openclassVersionControl {
funisBadVersion(version: Int): Boolean = TODO()
}
classSolution : VersionControl() {
funfirstBadVersion(n: Int): Int {
funhelper(left: Int, right: Int): Int {
if (left >= right) return left
val mid = left + (right - left) / 2returnif (isBadVersion(mid)) helper(left, mid) else helper(mid + 1, right)
}
return helper(1, n)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
# The isBadVersion API is already defined for you.# def isBadVersion(version: int) -> bool:classSolution:
deffirstBadVersion(self, n: int) -> int:
defhelper(left, right):
if left >= right:
return left
mid = left + (right - left) //2if isBadVersion(mid):
return helper(left, mid)
else:
return helper(mid +1, right)
return helper(1, n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// The is_bad_version API is defined for you.
// fn is_bad_version(version: i32) -> bool;
impl Solution {
pubfnfirst_bad_version(n: i32) -> i32 {
fnhelper(left: i32, right: i32) -> i32 {
if left >= right {
return left;
}
let mid = left + (right - left) /2;
if is_bad_version(mid) {
helper(left, mid)
} else {
helper(mid +1, right)
}
}
helper(1, n)
}
}
1
2
3
4
5
6
7
8
9
10
// The isBadVersion API is defined for you.
functionfirstBadVersion(n: number):number {
functionhelper(left: number, right: number):number {
if (left>=right) returnleft;
constmid= Math.floor(left+ (right-left) /2);
if (isBadVersion(mid)) returnhelper(left, mid);
elsereturnhelper(mid+1, right);
}
returnhelper(1, n);
}
Instead of recursion, we can use an iterative approach to binary search. We maintain two pointers (left and right) and repeatedly check the middle version. If the middle version is bad, we record it as a candidate and continue searching to the left. If not, we search to the right. This approach avoids recursion and uses constant space.
// The API isBadVersion(version) is defined for you.
classSolution {
public:int firstBadVersion(int n) {
int left =1, right = n, ans =-1;
while (left <= right) {
int mid = left + (right - left) /2;
if (isBadVersion(mid)) {
ans = mid;
right = mid -1;
} else {
left = mid +1;
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// The isBadVersion API is defined for you.funcfirstBadVersion(nint) int {
left, right, ans:=1, n, -1forleft<=right {
mid:=left+ (right-left)/2ifisBadVersion(mid) {
ans = midright = mid-1 } else {
left = mid+1 }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// The isBadVersion API is defined for you.publicclassSolutionextends VersionControl {
publicintfirstBadVersion(int n) {
int left = 1, right = n, ans =-1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
// The isBadVersion API is defined for you.
openclassVersionControl {
funisBadVersion(version: Int): Boolean = TODO()
}
classSolution : VersionControl() {
funfirstBadVersion(n: Int): Int {
var left = 1var right = n
var ans = -1while (left <= right) {
val mid = left + (right - left) / 2if (isBadVersion(mid)) {
ans = mid
right = mid - 1 } else {
left = mid + 1 }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
# The isBadVersion API is already defined for you.# def isBadVersion(version: int) -> bool:classSolution:
deffirstBadVersion(self, n: int) -> int:
left, right, ans =1, n, -1while left <= right:
mid = left + (right - left) //2if isBadVersion(mid):
ans = mid
right = mid -1else:
left = mid +1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// The is_bad_version API is defined for you.
// fn is_bad_version(version: i32) -> bool;
impl Solution {
pubfnfirst_bad_version(n: i32) -> i32 {
let (mut left, mut right, mut ans) = (1, n, -1);
while left <= right {
let mid = left + (right - left) /2;
if is_bad_version(mid) {
ans = mid;
right = mid -1;
} else {
left = mid +1;
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// The isBadVersion API is defined for you.
functionfirstBadVersion(n: number):number {
letleft=1, right=n, ans=-1;
while (left<=right) {
constmid= Math.floor(left+ (right-left) /2);
if (isBadVersion(mid)) {
ans=mid;
right=mid-1;
} else {
left=mid+1;
}
}
returnans;
}