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
相关推荐
sinat_2869451913 分钟前
spec vs plan ai coding
人工智能·深度学习·算法·chatgpt·prompt
Aaswk13 分钟前
蓝桥杯2025年第十六届省赛真题(更新中)
c语言·数据结构·c++·算法·职场和发展·蓝桥杯
舟舟亢亢24 分钟前
算法总结——【堆、堆排序】
算法
sali-tec43 分钟前
C# 基于OpenCv的视觉工作流-章35-组件连通
图像处理·人工智能·opencv·算法·计算机视觉
总斯霖1 小时前
P15445永远在一起!题解(月赛T2)
数据结构·c++·算法·深度优先
Frostnova丶1 小时前
LeetCode 3296. 使山区高度为零的最少秒数
算法·leetcode
会员源码网1 小时前
抽象数据类型(ADT):理论与实践的桥梁
算法
像污秽一样1 小时前
算法设计与分析-习题4.5
数据结构·算法·排序算法·剪枝
样例过了就是过了1 小时前
LeetCode热题100 全排列
数据结构·c++·算法·leetcode·dfs