First Bad Version

简介: 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.

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 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.

 

// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        int left=1;
        int right=n;
        while(left<=right)
        {
            int mid=left+((right-left)>>1);
            if(isBadVersion(mid))
            {
                if(mid==1||mid>1&&!isBadVersion(mid-1))
                    return mid;
                right=mid-1;
            }
            else
                left=mid+1;
        }
        return -1;
    }
};

 

相关文章
libtool: Version mismatch error 解决
libtool: Version mismatch error 解决
629 0
|
30天前
|
SQL 流计算
"Missing version in readMessageBegin, old client?
"Missing version in readMessageBegin, old client?
11 1
|
29天前
|
Java
Version 1.8.0_201 of the JVM is not suitable for this product. Version: 11 or greater is required.
Version 1.8.0_201 of the JVM is not suitable for this product. Version: 11 or greater is required.
13 0
|
3月前
|
SQL 流计算
Missing version in readMessageBegin, old client?"这个错误
Missing version in readMessageBegin, old client?"这个错误【1月更文挑战第22天】【1月更文挑战第107篇】
51 1
|
11月前
AttributeError: ‘version_info‘ object has no attribute ‘version‘
AttributeError: ‘version_info‘ object has no attribute ‘version‘
198 0
|
Java 编译器 应用服务中间件
Unsupported major.minor version 52.0(unable to load class com.xxxxxxx.xxx.xx.xx)
Unsupported major.minor version 52.0(unable to load class com.xxxxxxx.xxx.xx.xx)
117 0
Unsupported major.minor version 52.0(unable to load class com.xxxxxxx.xxx.xx.xx)
Execution failed for task :Test:lintVitalRelease/Lint found fatal errors while assembling a release
Execution failed for task :Test:lintVitalRelease/Lint found fatal errors while assembling a release
98 0
libtool: Version mismatch error
libtool: Version mismatch error
71 0
ERROR: cuvid requested, but not all dependencies are satisfied: ffnvcodec
ERROR: cuvid requested, but not all dependencies are satisfied: ffnvcodec
87 0
C compiler test failed.
C compiler test failed.
104 0