Skip to content

Commit 792f419

Browse files
committed
add solution of problem 278: first bad version
1 parent 71ec3a2 commit 792f419

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

FirstBadVersion278/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
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.
2+
3+
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.
4+
5+
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.

FirstBadVersion278/Solution.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* The isBadVersion API is defined in the parent class VersionControl.
2+
boolean isBadVersion(int version); */
3+
4+
public class Solution extends VersionControl {
5+
public int firstBadVersion(int n) {
6+
int left = 1;
7+
long right = n;
8+
9+
while (left <= right) {
10+
int mid = (int)((left + right) >> 1);
11+
if (isBadVersion(mid)) {
12+
right = mid - 1;
13+
} else {
14+
left = mid + 1;
15+
}
16+
}
17+
18+
return left;
19+
}
20+
}

0 commit comments

Comments
 (0)