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
相关推荐
小安同学iter4 分钟前
SQL50+Hot100系列(11.7)
java·算法·leetcode·hot100·sql50
_dindong11 分钟前
笔试强训:Week-4
数据结构·c++·笔记·学习·算法·哈希算法·散列表
星释23 分钟前
Rust 练习册 :Nucleotide Codons与生物信息学
开发语言·算法·rust
寂静山林40 分钟前
UVa 1366 Martian Mining
算法
陌路201 小时前
S12 简单排序算法--冒泡 选择 直接插入 希尔排序
数据结构·算法·排序算法
雾岛—听风2 小时前
P1012 [NOIP 1998 提高组] 拼数
算法
papership2 小时前
【入门级-算法-5、数值处理算法:高精度的乘法】
数据结构·算法
earthzhang20212 小时前
【1039】判断数正负
开发语言·数据结构·c++·算法·青少年编程
谈笑也风生2 小时前
只出现一次的数字 II(一)
数据结构·算法·leetcode
蕓晨2 小时前
auto 自动类型推导以及注意事项
开发语言·c++·算法