-
Notifications
You must be signed in to change notification settings - Fork 0
278_FirstBadVersion
a920604a edited this page Apr 14, 2023
·
1 revision
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int l =1, r = n;
while(l<=r){
int mid = l + (r-l)/2;
if(isBadVersion(mid)) r= mid-1;
else if(!isBadVersion(mid)) l = mid+1;
}
return l;
}
};
- lower bound
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int l =1, r = n;
while(l<r){
int mid = l + (r-l)/2;
if(isBadVersion(mid) ) r = mid;
else l = mid+1;
}
return l;
}
};
- time complexity
O(logn)
- space complexity
O(1)
footer