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
相关推荐
qq_4856689932 分钟前
算法习题--蓝桥杯
算法·职场和发展·蓝桥杯
waves浪游35 分钟前
类和对象(中)
c语言·开发语言·数据结构·c++·算法·链表
做人不要太理性40 分钟前
【算法一周目】滑动窗口(2)
c++·算法·leetcode·哈希算法·散列表·滑动窗口
青い月の魔女40 分钟前
数据结构初阶---复杂度
c语言·数据结构·笔记·学习·算法
汤姆和杰瑞在瑞士吃糯米粑粑40 分钟前
【优先算法学习】双指针--结合题目讲解学习
c++·学习·算法
m0_5474866641 分钟前
数据结构试题库1
java·数据结构·算法
多多*43 分钟前
后端并发编程操作简述 Java高并发程序设计 六类并发容器 七种线程池 四种阻塞队列
java·开发语言·前端·数据结构·算法·状态模式
摆烂小白敲代码1 小时前
【算法】连通块问题(C/C++)
c语言·数据结构·c++·算法·深度优先·图论·广度优先
香菜大丸1 小时前
详解 指针函数,函数指针,函数指针函数
开发语言·c++·算法
cv2016_DL1 小时前
BERT相关知识
人工智能·算法·transformer