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
相关推荐
ByteX21 小时前
算法练习-成功之后不许掉队
算法
蒙奇D索大21 小时前
【算法】 递归实战应用:从暴力迭代到快速幂的优化之路
笔记·考研·算法·改行学it
Miraitowa_cheems21 小时前
LeetCode算法日记 - Day 101: 最长公共子序列
数据结构·算法·leetcode·深度优先·动态规划
DuHz21 小时前
基于信号分解的FMCW雷达相互干扰抑制——论文阅读
论文阅读·算法·汽车·信息与通信·毫米波雷达
徐行tag21 小时前
RLS(递归最小二乘)算法详解
人工智能·算法·机器学习
南方的狮子先生1 天前
【C++】C++文件读写
java·开发语言·数据结构·c++·算法·1024程序员节
Alex艾力的IT数字空间1 天前
完整事务性能瓶颈分析案例:支付系统事务雪崩优化
开发语言·数据结构·数据库·分布式·算法·中间件·php
玖剹1 天前
二叉树递归题目(一)
c语言·c++·算法·leetcode
沧澜sincerely1 天前
BFS & 图论【各种题型+对应LeetCode习题练习】
leetcode·图论·广度优先
ChoSeitaku1 天前
线代强化NO6|矩阵|例题|小结
算法·机器学习·矩阵