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
相关推荐
gihigo19987 小时前
基于反步法的路径追踪控制
算法
Jim-2ha07 小时前
【JavaScript】常见排序算法实现
javascript·算法·排序算法
王老师青少年编程7 小时前
2025年12月GESP(C++二级): 黄金格
c++·算法·gesp·csp·信奥赛·二级·黄金格
Herbert_hwt7 小时前
C语言位操作符详解:从入门到实战应用
c语言·算法
ss2738 小时前
CompletionService:Java并发工具包
java·开发语言·算法
额呃呃8 小时前
select和poll之间的性能对比
开发语言·算法
王哈哈^_^8 小时前
【完整源码+数据集】道路交通事故数据集,yolo车祸检测数据集 7869 张,交通事故级别检测数据集,交通事故检测系统实战教程
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·毕业设计
星轨初途8 小时前
C++ string 类详解:概念、常用操作与实践(算法竞赛类)
开发语言·c++·经验分享·笔记·算法
先做个垃圾出来………8 小时前
53. 最大子数组和
算法·leetcode
POLITE38 小时前
Leetcode 160.相交链表 JavaScript (Day 9)
javascript·leetcode·链表