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
相关推荐
共享家95272 小时前
链表操作:分区与回文判断
c语言·开发语言·数据结构·leetcode·链表
ClaNNEd@2 小时前
大话数据结构第二章,算法笔记
数据结构·笔记·算法
圣保罗的大教堂4 小时前
《算法笔记》9.3小节——数据结构专题(2)->树的遍历 问题 A: 树查找
算法
自信的小螺丝钉6 小时前
Leetcode 1277. 统计全为 1 的正方形子矩阵 动态规划
leetcode·矩阵·动态规划
乌云暮年6 小时前
算法刷题整理合集(四)
java·开发语言·算法·dfs·bfs
Luo_LA7 小时前
【排序算法对比】快速排序、归并排序、堆排序
java·算法·排序算法
AI技术控7 小时前
机器学习算法实战——天气数据分析(主页有源码)
算法·机器学习·数据分析
oioihoii7 小时前
C++20 中的同步输出流:`std::basic_osyncstream` 深入解析与应用实践
c++·算法·c++20
猪猪成7 小时前
【图论】FLOYD弗洛伊德算法-最短路径
学习·算法·图论
HR Zhou7 小时前
群体智能优化算法-粒子群优化算法(Particle Swarm Optimization, PSO,含Matlab源代码)
算法·matlab·优化·智能优化算法