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
相关推荐
2501_9416233215 小时前
智慧农业监控平台中的多语言语法引擎与实时决策实践
leetcode
轻抚酸~16 小时前
KNN(K近邻算法)-python实现
python·算法·近邻算法
Yue丶越18 小时前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
小白程序员成长日记19 小时前
2025.11.24 力扣每日一题
算法·leetcode·职场和发展
有一个好名字19 小时前
LeetCode跳跃游戏:思路与题解全解析
算法·leetcode·游戏
AndrewHZ19 小时前
【图像处理基石】如何在图像中提取出基本形状,比如圆形,椭圆,方形等等?
图像处理·python·算法·计算机视觉·cv·形状提取
蓝牙先生20 小时前
简易TCP C/S通信
c语言·tcp/ip·算法
2501_9418705620 小时前
Python在高并发微服务数据同步与分布式事务处理中的实践与优化
leetcode
2501_941147711 天前
高并发微服务架构Spring Cloud与Dubbo在互联网优化实践经验分享
leetcode
稚辉君.MCA_P8_Java1 天前
Gemini永久会员 Java中的四边形不等式优化
java·后端·算法