第一个错误的版本_寻找第一个错误的版本
第一個錯誤的版本
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.
假設(shè)IncludeHelp變成一家產(chǎn)品公司,并且我們有一位產(chǎn)品經(jīng)理領(lǐng)導(dǎo)一個團(tuán)隊(duì)來開發(fā)新產(chǎn)品。 不幸的是,我們產(chǎn)品的最新版本未能通過質(zhì)量檢查。 由于每個版本都是基于先前版本開發(fā)的,因此錯誤版本之后的所有版本也都是錯誤的。
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.
假設(shè)我們有n個版本[1、2,...,n] ,我們想找出第一個不良版本,這將導(dǎo)致隨后的所有不良版本。
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?
我們的產(chǎn)品經(jīng)理會獲得一個API bool isBadVersion(version) ,它將返回版本是否正確。 他現(xiàn)在想雇用一名程序員來實(shí)現(xiàn)一個功能,以查找第一個不良版本。 對API的調(diào)用次數(shù)應(yīng)最少。 你能幫他嗎?
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.
當(dāng)然這是一個搜索問題,為了進(jìn)行優(yōu)化,我們可以在此處進(jìn)行二進(jìn)制搜索。 但是現(xiàn)在的問題是,對于搜索問題,還有其他最佳搜索方法嗎? 答案是肯定的。
It is binary search but the narrow down constant, K is not typically (low + high)/2 as in case of general binary search.
它是二進(jìn)制搜索,但縮小常數(shù) K通常不像一般二進(jìn)制搜索那樣(低+高)/ 2 。
In this case the narrow down constant, K= low+(high-low)/2 resulting in much more optimized result.
在這種情況下, 縮小常數(shù) K = low +(high-low)/ 2,從而導(dǎo)致更加優(yōu)化的結(jié)果。
Algorithm:
算法:
We have already the API function bool isBadVersion(version)
我們已經(jīng)有API函數(shù)bool isBadVersion(version)
Now to find the first bad version we generate another function:
現(xiàn)在找到第一個不良版本,我們生成另一個函數(shù):
low=lower bound variable
high=upper bound variable
低=下界變量
高=上限變量
C++ implementation
C ++實(shí)現(xiàn)
#include <bits/stdc++.h> using namespace std;#define n 10int 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 factorint mid=low+(high-low)/2;if(isBadVersion(mid)) {if(mid==0 || !isBadVersion(mid-1))return mid+1;elsehigh=mid-1;}elselow=mid+1;}return -1; }int firstBadVersion(int i) {if(i==1){if(isBadVersion(i))return i+1;elsereturn -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 productcout<<"product versions are:\n";for(int i=0;i<n;i++){cout<<i+1<<"\t";if(A[i])cout<<"bad"<<endl;elsecout<<"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
第一個錯誤的版本
總結(jié)
以上是生活随笔為你收集整理的第一个错误的版本_寻找第一个错误的版本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: node oauth2验证_如何设置和使
- 下一篇: scala 拆分字符串翻转_Scala程