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
相关推荐
wefg111 分钟前
【算法】约数个数、约数和
算法
波哥学开发21 分钟前
自动驾驶必备:全面解析鱼眼相机投影模型(UCM/KB/DS)及实战代码
算法
云泽80824 分钟前
蓝桥杯算法精讲:二分算法之二分答案深度剖析
算法·蓝桥杯
小龙报26 分钟前
【数据结构与算法】环与相遇:链表带环问题的底层逻辑与工程实现
c语言·数据结构·c++·物联网·算法·链表·visualstudio
佩奇大王28 分钟前
P2118 排列字母
java·开发语言·算法
仟濹33 分钟前
【算法打卡day20(2026-03-12 周四)算法:前缀和,二维前缀和,快慢指针,哈希表set使用技巧,哈希表map使用技巧】7个题
数据结构·算法
一叶落43836 分钟前
LeetCode 67. 二进制求和(C语言详解 | 双指针模拟加法)
c语言·数据结构·算法·leetcode
寒月小酒1 小时前
3.12 OJ
算法
CoovallyAIHub1 小时前
纯合成数据训练,真实图像Pose mAP达0.97:亚琛工大用YOLOv11实现风电关键点检测
深度学习·算法·计算机视觉