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
相关推荐
爱吃rabbit的mq10 小时前
第7章 逻辑回归:二分类的基础
算法·分类·逻辑回归
DFT计算杂谈10 小时前
VASP+Wannier90 计算位移电流和二次谐波SHG
java·服务器·前端·python·算法
执着25910 小时前
力扣102、二叉树的层序遍历
数据结构·算法·leetcode
Tisfy10 小时前
LeetCode 2976.转换字符串的最小成本 I:floyd算法(全源最短路)
算法·leetcode··floyd·题解
v_for_van10 小时前
力扣刷题记录4(无算法背景,纯C语言)
c语言·算法·leetcode
dazzle10 小时前
Python数据结构(十五):归并排序详解
数据结构·python·算法
2301_7644413310 小时前
基于paCy模型与jsoncrack进行依存句法分析
python·算法·自然语言处理
咩咩不吃草11 小时前
【逻辑回归】:从模型训练到评价
算法·机器学习·逻辑回归
ersaijun11 小时前
机器人运动控制关键算法体系:从理论框架到前沿实践
算法·机器人