第一个错误的版本

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
.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 ++实现

#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

第一个错误的版本

第一个错误的版本_寻找第一个错误的版本相关推荐

  1. 查看Python的版本_查看当前安装Python的版本

    一.查看Python的版本_查看当前安装Python的版本 具体方法: 首先按[win+r]组合键打开运行: 然后输入cmd,点击[确定]: 最后执行[python --version]命令即可. 特 ...

  2. python寻找质数_寻找质数方法Python2.7版本

    这是个人的Python代码练习作品,完成了在Python2.7版本下寻找质数方法的操作. #!/usr/bin/python # coding=utf8 # Find Primes # Usage: ...

  3. idea怎么看jdk版本_怎么看自己的jdk版本

    这个主要是在cmd下输入java-version来查看,64位的效果如下:如果没有标明是多少位的,默认一般是32位的,希望对你有用,我是ndk吧吧主,有问题可以ndk吧留言,谢谢! . 就安装最新版就 ...

  4. java 对应sql驱动版本_有关sqlserver的 jdbc驱动版本整理

    原标题:有关sqlserver的 jdbc驱动版本整理 皕杰报表创建sqlserver数据库的时候,需要加载sqlserver jdbc驱动,有些时候你需要了解不同版本的sqlserver的 jdbc ...

  5. js 随机1-10随机数_寻找随机的错误-一个真实的故事

    js 随机1-10随机数 几周前,我完成了RapidFTR开源项目的错误查找 ,这花了我三个晚上. 我认为可能值得分享狩猎的故事. 本文将介绍我的工作. 我将概述我的旅程,以便真正找到正在发生的事情的 ...

  6. 文件夹错误 分配句柄_重启数据库遇到错误ORA27154,ORA27300,ORA27301,ORA27302

    作者 | JiekeXu 来源 | JiekeXu之路(ID: JiekeXu_IT) 转载请联系授权 | (微信ID:xxq1426321293) 大家好,我是 JiekeXu,很高兴又和大家见面了 ...

  7. fiddler设置中文版本_教你下载iOS老版本应用

    大家都知道,苹果一旦升级了新版本的系统,基本上是不可能回滚到旧版本的.App 也是一样,升级了新版本后,官方也是没有提供旧版本回滚安装的!小狐在此也是非常懊恼. 那么有些小伙伴觉得新版本 App 难用 ...

  8. fiddler设置中文版本_突破安卓7.0以上版本WX小程序抓包篇

    工作上经常需要使用brupsuite抓取APP和WX小程序的包,所以会在安卓上安装burp的证书.但是你会发现安卓7.0之后有了network-security-config这个选项,可以让app只信 ...

  9. dedecms怎么改php版本_王者荣耀:管你版本怎么改,这几位峡谷常青树始终屹立不倒...

    作为MOBA对抗类游戏,王者荣耀的版本更迭一直很快,这使得英雄热度更迭也很快.许多此版本很强势热门的英雄,下一次版本更新直接被削一大截,沦为下水道英雄.玩家口中的一代版本一代神由此而来.但有些英雄无论 ...

最新文章

  1. 免费申领Bio-protocol单细胞研究实验方法精选集
  2. 图解Oracle备份方式分类
  3. javascript简单的四则运算
  4. 循环神经网络(RNN, Recurrent Neural Networks)介绍
  5. Vue+Openlayers+el-checkbox实现多选配置图层的显示和隐藏
  6. 【组原】广州大学计算机组成原理考试部分题+复习资料(2020-2021)
  7. Java 中文乱码问题
  8. 1438.最小公倍数
  9. BZOJ4477: [Jsoi2015]字符串树
  10. 无线通信设备安装工程概预算编制_电气设备安装工程计价与应用
  11. SEM搜索引擎竞价全方位系统网课-优就业-专题视频课程
  12. mysql万能密码_万能密码:‘or 1=1-- 实战SQL注入,秒破后台
  13. python3.7爬取墨菲定律保存在本地txt
  14. 100---Python绘制圆锥体
  15. 亚马逊运营教程,三招学会亚马逊
  16. 一文读懂POE交换机和普通交换机的区别
  17. 半加器设计(结构描述法)
  18. 22.IO引脚复用和映射原理与配置
  19. 爬虫那些事-网页爬虫设计思路
  20. 合合信息——用智能文字识别技术赋能古彝文原籍数字化

热门文章

  1. SQLExecption:Operation not allowed after ResultSet closed解决办法
  2. java string的作用_浅谈java String不可变的好处
  3. java替换图片中文字_Java 添加、替换、删除Word中的图片
  4. 一个android工程生成两个aar,android studio生成aar包并在其他工程引用aar包(示例代码)...
  5. checkA.php,php window平台模拟checkdnsrr函数检测_php
  6. java安全(二):JDBC|sql注入|预编译
  7. python 执行shell_python执行shell命令的方法
  8. MIP 官方发布 v1稳定版本
  9. Problem D: 链表的基本运算(线性表)
  10. Material Design之AppBarLayout总结