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
相关推荐
wydaicls40 分钟前
C语言对单链表的操作
c语言·数据结构·算法
傻童:CPU43 分钟前
C语言需要掌握的基础知识点之排序
c语言·算法·排序算法
大数据张老师5 小时前
数据结构——邻接矩阵
数据结构·算法
低音钢琴6 小时前
【人工智能系列:机器学习学习和进阶01】机器学习初学者指南:理解核心算法与应用
人工智能·算法·机器学习
傻童:CPU8 小时前
C语言需要掌握的基础知识点之前缀和
java·c语言·算法
又见野草8 小时前
软件设计师知识点总结:数据结构与算法(超级详细)
数据结构·算法·排序算法
GalaxyPokemon8 小时前
有一个服务器,用于提供HTTP服务,但是需要限制每个用户在任意的100秒内只能请求60次,怎么实现这个功能
算法
fl1768319 小时前
基于opencv+Mediapipe+CNN实现用手势识别控制对鼠标操控python源码+项目说明+设计文档
算法
K 旺仔小馒头9 小时前
优选算法:01 双指针巧解移动零问题
c++·算法·刷题
sali-tec10 小时前
C# 基于halcon的视觉工作流-章49-网面破损
开发语言·图像处理·算法·计算机视觉·c#