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
相关推荐
D_FW21 小时前
数据结构第六章:图
数据结构·算法
a程序小傲21 小时前
京东Java面试被问:动态规划的状态压缩和优化技巧
java·开发语言·mysql·算法·adb·postgresql·深度优先
自学不成才1 天前
深度复盘:一次flutter应用基于内存取证的黑盒加密破解实录并完善算法推理助手
c++·python·算法·数据挖掘
June`1 天前
全排列与子集算法精解
算法·leetcode·深度优先
徐先生 @_@|||1 天前
Palantir Foundry 五层架构模型详解
开发语言·python·深度学习·算法·机器学习·架构
夏鹏今天学习了吗1 天前
【LeetCode热题100(78/100)】爬楼梯
算法·leetcode·职场和发展
圣保罗的大教堂1 天前
leetcode 712. 两个字符串的最小ASCII删除和 中等
leetcode
m0_748250031 天前
C++ 信号处理
c++·算法·信号处理
Ro Jace1 天前
电子侦察信号处理流程及常用算法
算法·信号处理