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
相关推荐
健忘的派大星1 小时前
需求激增800%!2025年第一硬通货:懂大模型、云计算和硬件的“前沿部署工程师”!
人工智能·算法·架构·langchain·云计算·大模型学习·大模型教程
ShineWinsu8 小时前
对于C++:继承的解析—上
开发语言·数据结构·c++·算法·面试·笔试·继承
pp起床8 小时前
动态规划 | part05
算法·动态规划
GuangHeAI_ATing8 小时前
国密算法SSD怎么选?这3款国产固态硬盘安全又高速
算法
雨泪丶9 小时前
代码随想录算法训练营-Day34
算法
Yzzz-F9 小时前
牛客寒假算法训练营2
算法
甄心爱学习9 小时前
【python】获取所有长度为 k 的二进制字符串
python·算法
iAkuya9 小时前
(leetcode)力扣100 76数据流的中位数(堆)
算法·leetcode·职场和发展
键盘鼓手苏苏10 小时前
Flutter for OpenHarmony: Flutter 三方库 ntp 精准同步鸿蒙设备系统时间(分布式协同授时利器)
android·分布式·算法·flutter·华为·中间件·harmonyos
董董灿是个攻城狮10 小时前
AI 视觉连载5:传统 CV 之均值滤波
算法