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
相关推荐
NAGNIP3 分钟前
面试官:给我讲一下卷积吧!
算法·面试
2401_831824967 分钟前
高性能计算集群部署
开发语言·c++·算法
NAGNIP9 分钟前
一文搞懂卷积神经网络!
算法·面试
菜鸟小九9 分钟前
hot100(91-100)
数据结构·算法·排序算法
add45a21 分钟前
C++代码移植性设计
开发语言·c++·算法
ccLianLian24 分钟前
leetcode-hot100
算法·leetcode·职场和发展
qq_1481153726 分钟前
分布式系统容错设计
开发语言·c++·算法
m0_5603964728 分钟前
C++中的享元模式
开发语言·c++·算法
左左右右左右摇晃31 分钟前
数据结构——数组
数据结构·笔记·算法
nainaire35 分钟前
速通LeetCode hot100——(1~9 哈希,双指针,滑动窗口)
c++·笔记·算法·leetcode