第一个错误的版本_寻找第一个错误的版本

第一个错误的版本

Problem statement:

问题陈述:

Suppose that IncludeHelp turns to be a product company & we have a product manager leading a team to develop a new product. Unfortunately, the latest version of our 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.

假设IncludeHelp变成一家产品公司,并且我们有一位产品经理领导一个团队来开发新产品。 不幸的是,我们产品的最新版本未能通过质量检查。 由于每个版本都是基于先前版本开发的,因此错误版本之后的所有版本也都是错误的。

Suppose we have n versions [1, 2, ..., n] and we want to find out the first bad one, which causes all the following ones to be bad.

假设我们有n个版本[1、2,...,n] ,我们想找出第一个不良版本,这将导致随后的所有不良版本。

Our product manager is given an API bool isBadVersion(version) which will return whether version is bad or not. He now wants to hire a programmer to implement a function to find the first bad version. There should be minimum number of calls to the API. Can you help him out?

我们的产品经理会获得一个API bool isBadVersion(version) ,它将返回版本是否正确。 他现在想雇用一名程序员来实现一个功能,以查找第一个不良版本。 对API的调用次数应最少。 你能帮他吗?

Solution:

解:

Of course this is a searching problem & for optimization we can do a binary search here. But now the question is, is there any other optimum searching method for search problem? The answer is yes.

当然这是一个搜索问题,为了进行优化,我们可以在此处进行二进制搜索。 但是现在的问题是,对于搜索问题,还有其他最佳搜索方法吗? 答案是肯定的。

It is binary search but the narrow down constant, K is not typically (low + high)/2 as in case of general binary search.

它是二进制搜索,但缩小常数 K通常不像一般二进制搜索那样(低+高)/ 2 。

In this case the narrow down constant, K= low+(high-low)/2 resulting in much more optimized result.

在这种情况下, 缩小常数 K = low +(high-low)/ 2,从而导致更加优化的结果。

Algorithm:

算法:

We have already the API function bool isBadVersion(version)

我们已经有API函数bool isBadVersion(version)

Now to find the first bad version we generate another function:

现在找到第一个不良版本,我们生成另一个函数:

low=lower bound variable
high=upper bound variable

低=下界变量
高=上限变量

FUNCTION findFirstBad( int low, int high)
    While(low<=high){
        1.  Set mid to the narrow down constant K, low+(high-low)/2;
        2.  IF (isBadVersion(mid))  //if it is bad
            //there can be two case
            //1.    This is no 1, so thdere is no other version previously 
            //      thus these must be the first one
            //2.    The immediate previous one is not bad, thus this is 
            //      the first bad one
            IF (mid==1 || !isBadVersion(mid-1))
                Return mid;
            ELSE
                //narrow down high as first bad one must be before this one
                high=mid-1;
            End IF-ELSE
        ELSE
            //since this is not bad, the lower bound must be > this version
            low=mid+1;
        END IF-ELSE
        3.  If not returned from while loop, no bad version exists at all
        Return -1;
    END WHILE
END FUNCTION


C++ implementation

C ++实现

#include <bits/stdc++.h>
using namespace std;

#define n 10

int A[n]= { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};

// declaration of isBadVersion API.
bool isBadVersion(int version){
	return A[version];
}

int findFirstBad(int low,int high){
	while(low<=high){
		//narrow down factor
		int mid=low+(high-low)/2;
		if(isBadVersion(mid)) {
			if(mid==0 || !isBadVersion(mid-1))
				return mid+1;
			else
				high=mid-1;
		}
		else
			low=mid+1;
	}
	return -1;	
}

int firstBadVersion(int i) {
	if(i==1){
		if(isBadVersion(i))
			return i+1;
		else
			return -1;
	}
	return findFirstBad(0,i);
}

int main(){
	cout<<"this is a functional problem,so main functiom hardcoded\n";
	//product versions
	//A[n]= { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
	//0=good product, 1=bad product
	cout<<"product versions are:\n";
	for(int i=0;i<n;i++){
		cout<<i+1<<"\t";
		if(A[i])
			cout<<"bad"<<endl;
		else
			cout<<"good"<<endl;
	}
	
	cout<<"First Bad version is:\n";
	cout<<firstBadVersion(n-1);
	
	return 0;
}

Output

输出量

this is a functional problem,so main functiom hardcoded
product versions are:
1       good
2       good
3       good
4       good
5       good
6       good
7       bad
8       bad
9       bad
10      bad
First Bad version is:
7  


翻译自: https://www.includehelp.com/icp/finding-first-bad-version.aspx

第一个错误的版本

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值