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
相关推荐
xushichao198927 分钟前
C++中的中介者模式
开发语言·c++·算法
MORE_7729 分钟前
leecode100-买卖股票的最佳时期-贪心算法
python·算法·贪心算法
AI科技星33 分钟前
基于wr/c + h/c = 1的螺旋线矢量特性及应用分析
c语言·开发语言·人工智能·opencv·算法·计算机视觉·r语言
颜酱35 分钟前
回溯算法专项突破练习(1)
javascript·后端·算法
进击的荆棘44 分钟前
优选算法——分治
数据结构·算法·leetcode·分治
Yupureki1 小时前
《实战项目-个人在线OJ平台》1.项目简介和演示
c语言·数据结构·c++·sql·算法·性能优化·html5
m0_579393661 小时前
C++代码混淆与保护
开发语言·c++·算法
qq_148115371 小时前
C++中的享元模式实战
开发语言·c++·算法
Yzzz-F1 小时前
Problem - D2 - Codeforces
算法
烟花巷子1 小时前
C++中的解释器模式
开发语言·c++·算法