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
相关推荐
有Li18 分钟前
一种病理学内容感知变速率学习图像压缩框架 (PathoLIC)/文献速递-多模态应用技术
人工智能·深度学习·算法·计算机视觉·医学生
x_xbx31 分钟前
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
Ricky_Theseus40 分钟前
数据库关系代数 - 连接操作
linux·数据库·算法
绿算技术1 小时前
宝辰股份董事长莅临绿算技术调研交流
人工智能·科技·算法
码云数智-园园1 小时前
哈希冲突的解决之道:深入理解哈希表底层原理
算法·哈希算法
qq_416018721 小时前
C++中的模板方法模式
开发语言·c++·算法
天上路人2 小时前
A-59F 多功能语音处理模组在本地会议系统扩音啸叫处理中的技术应用与性能分析
人工智能·神经网络·算法·硬件架构·音视频·语音识别·实时音视频
yang_B6212 小时前
噪声处理方法
大数据·人工智能·算法
菜菜小狗的学习笔记2 小时前
剑指Offer算法题(九)搜索
数据结构·算法·深度优先
JosieBook2 小时前
【C#】C# 所有关键字总结
开发语言·算法·c#