Leetcode 278. First Bad Version

Problem

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 returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Algorithm

Bineary search.

Code

python3 复制代码
class Solution:
    def firstBadVersion(self, n: int) -> int:
        L, R, = 0, n - 1
        while L < R:
            Mid = (L + R + 1) // 2
            if isBadVersion(Mid):
                R = Mid - 1
            else: L = Mid
        return L + 1
相关推荐
参.商.5 分钟前
【Day48】46. 全排列
leetcode·golang
不熬夜的熬润之8 分钟前
APCE-平均峰值相关能量
人工智能·算法·计算机视觉
yzx99101310 分钟前
实时数据流处理实战:从滑动窗口算法到Docker部署
算法·docker·容器
佩奇大王32 分钟前
P674 三羊献瑞
算法·深度优先·图论
发疯幼稚鬼1 小时前
大整数乘法运算
c语言·算法
宵时待雨2 小时前
C++笔记归纳17:哈希
数据结构·c++·笔记·算法·哈希算法
问好眼2 小时前
《算法竞赛进阶指南》0x05 排序-1.电影
c++·算法·排序·信息学奥赛
CoderCodingNo2 小时前
【GESP】C++八级考试大纲知识点梳理 (6) 图论算法:最小生成树与最短路
c++·算法·图论
DeepModel2 小时前
【特征选择】嵌入法(Embedded)
人工智能·python·深度学习·算法
今儿敲了吗2 小时前
算法复盘——前缀和
笔记·学习·算法