-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstBadVersion.java
37 lines (32 loc) · 1.18 KB
/
firstBadVersion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* The isBadVersion API is defined in the parent class VersionControl.
* boolean isBadVersion(int version);
*/
public class Solution extends VersionControl {
// Method to find the first bad version
public int firstBadVersion(int n) {
// Initialize left pointer to the first version
int left = 1;
// Initialize right pointer to the last version
int right = n;
int mid; // Declare variable to store the middle version
// If there's only one version, return it immediately
if(right == left)
return left;
// Perform binary search to find the first bad version
while (left < right) {
// Calculate the middle version to check
mid = ((right - left) / 2) + left;
// Check if the middle version is bad
if (isBadVersion(mid)) {
// Narrow down the search range to the left half
right = mid;
} else {
// Narrow down the search range to the right half
left = mid + 1;
}
}
// Return the left pointer, which points to the first bad version
return left;
}
}