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
相关推荐
清酒难咽1 天前
算法案例之递归
c++·经验分享·算法
让我上个超影吧1 天前
【力扣26&80】删除有序数组中的重复项
算法·leetcode
张张努力变强1 天前
C++ Date日期类的设计与实现全解析
java·开发语言·c++·算法
沉默-_-1 天前
力扣hot100滑动窗口(C++)
数据结构·c++·学习·算法·滑动窗口
钱彬 (Qian Bin)1 天前
项目实践19—全球证件智能识别系统(优化检索算法:从MobileNet转EfficientNet)
算法·全球证件识别
feifeigo1231 天前
基于EM算法的混合Copula MATLAB实现
开发语言·算法·matlab
漫随流水1 天前
leetcode回溯算法(78.子集)
数据结构·算法·leetcode·回溯算法
IT猿手1 天前
六种智能优化算法(NOA、MA、PSO、GA、ZOA、SWO)求解23个基准测试函数(含参考文献及MATLAB代码)
开发语言·算法·matlab·无人机·无人机路径规划·最新多目标优化算法
We་ct1 天前
LeetCode 151. 反转字符串中的单词:两种解法深度剖析
前端·算法·leetcode·typescript
芜湖xin1 天前
【题解-Acwing】AcWing 5579. 增加模数(TLE)
算法·快速幂