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
相关推荐
wearegogog1232 分钟前
光谱分析波段选择的连续投影算法
算法
执笔论英雄6 分钟前
【RL】DAPO 数据处理
算法
why1511 小时前
面经整理——算法
java·数据结构·算法
悦悦子a啊1 小时前
将学生管理系统改造为C/S模式 - 开发过程报告
java·开发语言·算法
痕忆丶1 小时前
双线性插值缩放算法详解
算法
_codemonster2 小时前
深度学习实战(基于pytroch)系列(四十八)AdaGrad优化算法
人工智能·深度学习·算法
鹿角片ljp3 小时前
力扣140.快慢指针法求解链表倒数第K个节点
算法·leetcode·链表
自由生长20243 小时前
位运算第1篇-异或运算-快速找出重复数字
算法
xxxxxmy3 小时前
同向双指针(滑动窗口)
python·算法·滑动窗口·同向双指针
释怀°Believe3 小时前
Daily算法刷题【面试经典150题-5️⃣图】
算法·面试·深度优先