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
相关推荐
玲娜贝儿--努力学习买大鸡腿版7 分钟前
hot 100 刷题记录(1)
数据结构·python·算法
123过去32 分钟前
pixiewps使用教程
linux·网络·测试工具·算法·哈希算法
深圳市快瞳科技有限公司38 分钟前
低空经济下,鸟类识别算法与无人机硬件的兼容性优化策略
算法·无人机
努力中的编程者1 小时前
二叉树(C语言底层实现)
c语言·开发语言·数据结构·c++·算法
鹤旗1 小时前
While语句,do-while语句,for语句
java·jvm·算法
qq_416018721 小时前
高性能密码学库
开发语言·c++·算法
NAGNIP1 小时前
一文搞懂卷积神经网络经典架构-LeNet
算法·面试
宵时待雨1 小时前
C++笔记归纳14:AVL树
开发语言·数据结构·c++·笔记·算法
NAGNIP1 小时前
一文搞懂深度学习中的池化!
算法·面试
山川行2 小时前
关于《项目C语言》专栏的总结
c语言·开发语言·数据结构·vscode·python·算法·visual studio code