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
相关推荐
郝学胜-神的一滴34 分钟前
当AI遇见架构:Vibe Coding时代的设计模式复兴
开发语言·数据结构·人工智能·算法·设计模式·架构
Frostnova丶6 小时前
LeetCode 190.颠倒二进制位
java·算法·leetcode
骇城迷影6 小时前
代码随想录:链表篇
数据结构·算法·链表
专注前端30年7 小时前
智能物流路径规划系统:核心算法实战详解
算法
json{shen:"jing"}7 小时前
字符串中的第一个唯一字符
算法·leetcode·职场和发展
追随者永远是胜利者8 小时前
(LeetCode-Hot100)15. 三数之和
java·算法·leetcode·职场和发展·go
程序员酥皮蛋9 小时前
hot 100 第二十七题 27.合并两个有序链表
数据结构·leetcode·链表
BlockWay9 小时前
西甲赛程搬进平台:WEEX以竞猜开启区域合作落地
大数据·人工智能·算法·安全
im_AMBER10 小时前
Leetcode 121 翻转二叉树 | 二叉树中的最大路径和
数据结构·学习·算法·leetcode
mit6.82411 小时前
二分+贪心
算法