278. First Bad Version
题目:
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的更多相关文章
- 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导致的溢 ...
- 【leetcode】278. First Bad Version
problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...
- 278. First Bad Version - LeetCode
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...
- 【leetcode❤python】 278. First Bad Version
#-*- coding: UTF-8 -*-# The isBadVersion API is already defined for you.# @param version, an integer ...
- leetcode 278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- (medium)LeetCode 278.First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Leetcode 278 First Bad Version 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- Java [Leetcode 278]First Bad Version
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- 【easy】278. First Bad Version
有一系列产品,从某个开始其后都不合格,给定一个判断是否合格的函数,找出N个产品中第一个不合格的产品. 正确答案: // Forward declaration of isBadVersion API. ...
随机推荐
- SQL Server Reporting Services – Insufficient Rights Error
http://www.sql-server-performance.com/2011/security-ssrs-reporting-error/ SQL Server Reporting Servi ...
- centos中设置apache显示目录列表
apache中显示目录列表 在http.conf中加入如下代码(如有虚拟主机配置,加在虚拟主机配置段内),并把主目录内的index.pho,index.html,index.htm文件删除 复制代码 ...
- android开发 wifi开发不稳定性测试
场景:工厂定制机器,要求一个设备创建wifi热点,一个设备去连接.但是现在发现wifi连接很不稳定,主要以下3方面: 1.连接之前,不容易连接上 2.连接上之后,连不到外网 3.连接上之后,稳定性不好 ...
- RobotFramework-关键字
地址:https://github.com/NitorCreations/RobotFramework-EclipseIDE/tree/master/plugin/robot-indices Coll ...
- Jquery方法的应用
<body> <div id="one"><span>one</span></div><div class=&qu ...
- linshiwendang12--匈牙利
#include<bits/stdc++.h> #define N 10007 using namespace std; vector<int> p[N]; bool vis[ ...
- ptypes中string类的空间分配
问题描述: 在学习ptypes中string类的空间分配时,经常使分配的空间超出实际所需的空间 使用的分配函数是:_alloc函数 注: 在_alloc函数中调用了 ...
- BZOJ2879 [Noi2012]美食节
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2879 这题codevs上也有,不过数据不同:http://codevs.cn/proble ...
- 【BZOJ】【1048】【HAOI2007】分割矩阵
DP/记忆化搜索 暴力枚举分割方案?……大概是指数级的?大约是20!的方案= =? 但是我们看到a.b.n的范围都很小……所以不同的状态数只是$10^5$级别的,可以记忆化搜索求解 比较水的一道题…… ...
- 通过HTTP访问网络资源
添加访问网络的权限:<uses-permission android:name="android.permission.INTERNET"/> package com. ...