日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/11 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第一个错误的版本_寻找第一个错误的版本 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

第一個錯誤的版本

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

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

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 oneIF (mid==1 || !isBadVersion(mid-1))Return mid;ELSE//narrow down high as first bad one must be before this onehigh=mid-1;End IF-ELSEELSE//since this is not bad, the lower bound must be > this versionlow=mid+1;END IF-ELSE3. If not returned from while loop, no bad version exists at allReturn -1;END WHILE END FUNCTION .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。