题目:

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.

链接: http://leetcode.com/problems/first-bad-version/

题解:

找到First Bad Version,这里我们使用Binary Search就可以了。

Time Complexity - O(logn), Space Complexity - O(1)

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int lo = 1, hi = n; while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
if(isBadVersion(mid)) {
hi = mid - 1;
} else {
lo = mid + 1;
}
} return lo;
}
}

二刷:

二分搜索查找bad version的左边界。在找到badversion的时候我们让hi = mid - 1,否则lo = mid + 1,  最后返回lo就是第一个badversion的地方。

Java:

Time Complexity - O(logn), Space Complexity - O(1)

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
if (n < 1) {
return 1;
}
int lo = 1, hi = n;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (!isBadVersion(mid)) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return lo;
}
}

三刷:

Binary Search查找左边界。举个例子 {g, b, b, b, b}就很好理解了。lo和hi相等时hi--,所以我们最后返回lo就可以了

Java:

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
if (n < 2) return 1;
int lo = 1, hi = n;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (isBadVersion(mid)) hi = mid - 1;
else lo = mid + 1;
}
return lo;
}
}

Update:

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
if (n < 1) return n;
int lo = 1, hi = n;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (isBadVersion(mid)) hi = mid - 1;
else lo = mid + 1;
}
return lo;
}
}

Reference:

278. First Bad Version的更多相关文章

  1. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  2. 【leetcode】278. First Bad Version

    problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...

  3. 278. First Bad Version - LeetCode

    Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...

  4. 【leetcode❤python】 278. First Bad Version

    #-*- coding: UTF-8 -*-# The isBadVersion API is already defined for you.# @param version, an integer ...

  5. leetcode 278. First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  6. (medium)LeetCode 278.First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  7. Leetcode 278 First Bad Version 二分查找(二分下标)

    题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...

  8. Java [Leetcode 278]First Bad Version

    题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...

  9. 【easy】278. First Bad Version

    有一系列产品,从某个开始其后都不合格,给定一个判断是否合格的函数,找出N个产品中第一个不合格的产品. 正确答案: // Forward declaration of isBadVersion API. ...

随机推荐

  1. 闪回flashback

    1.flashback query(使用UNDO)查询某个scn时该表的内容 SQL> select current_scn ; 已更新 行. ;        //查询之前scn时的值 ID ...

  2. javascript面向对象分层思维

    js本身不是面向对象语言,在我们实际开发中其实很少用到面向对象思想,以前一直以为当要复用的时候才封装成对象,然而随着现在做的项目都后期测试阶段发现面向对象的作用不仅仅只是复用,可能你们会说面向对象还有 ...

  3. Team Homework #2 Decide the roles of each team member ——IloveSE

    大家好,我们是IloveSEers! 徐姗,我是一个性格开朗,但却认为计算机比较枯燥的女生.经过两年的学习,自己的编程能力,并不是很强,在这方便还需多多练习.对于软件工程这门课,我充满期待,因为我不仅 ...

  4. Visual Studio 2012 [ADO.NET 实体数据模型]丢失没有的解决方法

    首先打开控制面板,看是否已经安装EF,如果已经安装,先卸载,然后,首先打开安装包,找到/packages/EFTools目录下的EFTools.msi,将它们复制自己计算机的某一目录下,例如:C:\t ...

  5. 如何在Android模拟器上安装apk文件

    1.运行SDK Manager,选择模拟器,并运行模拟器 SDK Manager应用 2.将需要安装的apk文件复制到platform-tools目录下(默认在:D:\tools\android\ad ...

  6. SASS学习笔记_02

    导入 当模块化布局的时候 导入头和尾 私有化 不生成css文件 文件名前面加下划线   结果   嵌套导入   导入css文件 不推荐   注释 和默认变量值

  7. bzoj 1270 DP

    w[i,j]代表高度j,第i颗树的时候的最大值 那么w[i,j]:=max(w[i,j+1],w[k,j+heigh])+sum[i,j]: 但是这样枚举是n^3的,我们发现转移的第二个选择w[k,j ...

  8. 【BZOJ】【1863】【ZJOI2006】trouble 皇帝的烦恼

    二分+DP Orz KuribohG 神题啊= = 满足单调性是比较显然的…… 然而蒟蒻并不会判断能否满足……QwQ 神一样的DP姿势:f[i]表示第 i 个与第1个最多有多少个相同,g[i]表示最少 ...

  9. __int64 与long long 的区别 分类: Brush Mode 2014-08-14 10:22 64人阅读 评论(0) 收藏

    //为了和DSP兼容,TSint64和TUint64设置成TSint40和TUint40一样的数 //结果VC中还是认为是32位的,显然不合适 //typedef signed long int    ...

  10. C#简单windows服务

    因为做后台比较多,所以经常需要写一些后台服务.一般的流程是先创建一个服务项目,加入代码.然后打包一个安装程序或者直接用dos命令安装服务.下面是详细内容:    1. 创建windows服务项目.  ...