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
相关推荐
REDcker19 分钟前
Redis容灾策略与哈希槽算法详解
redis·算法·哈希算法
福楠41 分钟前
C++ STL | map、multimap
c语言·开发语言·数据结构·c++·算法
Sarvartha1 小时前
二分查找学习笔记
数据结构·c++·算法
难得的我们1 小时前
C++与区块链智能合约
开发语言·c++·算法
diediedei2 小时前
C++编译期正则表达式
开发语言·c++·算法
夏鹏今天学习了吗2 小时前
【LeetCode热题100(97/100)】二叉搜索树中第 K 小的元素
算法·leetcode·职场和发展
炽烈小老头2 小时前
【 每天学习一点算法 2026/01/26】缺失数字
学习·算法
小桃酥ღ2 小时前
[力扣每日习题][1339]. 分裂二叉树的最大乘积 2026.01.07
算法·leetcode·职场和发展
hrrrrb4 小时前
【算法设计与分析】贪心算法
算法·贪心算法·代理模式
TracyCoder1234 小时前
LeetCode Hot100(10/100)—— 53. 最大子数组和
算法·leetcode