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
相关推荐
Дерек的学习记录5 小时前
C++:入门基础(下)
开发语言·数据结构·c++·学习·算法·visualstudio
yugi9878385 小时前
无线传感器网络中GAF算法节点特性分析
网络·算法
1027lonikitave5 小时前
使用斐波那契数列讲解尾递归
算法
滴滴答滴答答6 小时前
LeetCode Hot100 之 16 合并两个有序链表
算法·leetcode·链表
ASKED_20197 小时前
企业级大模型微调(Fine-tuning)策略
大数据·人工智能·算法
圣保罗的大教堂7 小时前
leetcode 3713. 最长的平衡子串 I 中等
leetcode
t198751287 小时前
基于Chirp分解和多相快速算法的离散分数傅里叶变换(DFRFT)MATLAB实现
开发语言·算法·matlab
愚者游世7 小时前
力扣解决二进制 | 题型常用知识点梳理
c++·程序人生·算法·leetcode·职场和发展
阿星AI工作室8 小时前
宝藏skills!90个顶尖博客信源自动抓,AI每天帮我筛出20篇精华!
人工智能·算法
Ulyanov8 小时前
基于Python的单脉冲雷达导引头回波生成技术
python·算法·仿真·单脉冲雷达、